Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text file edit - multiple lines to one line 3

Status
Not open for further replies.

entrylevel

Technical User
Nov 29, 2001
46
CA
hi experts,

how to make the following output to one line, use sed or other thoughts? thanks.

original file

network-IP
netmask-IP
location

want to make it look like this

location network-IP netmask-IP

Regards!


 
Code:
BEGIN { RS = ""; FS="\n"; OFS = "  " }
{
  work = "ipNetwork:\t"
  mask = "ipNetmask:\t"
  L = "l:"
  for (i=1; i<=NF; i++)
  { if ( $i ~ /^ipNetwork/ )
      work = $i
    if ( $i ~ /^ipNetmask/ )
      mask = $i
    if ( $i ~ /^l:/ )
      L = $i
  }
  print work, mask, L
}
The output is
[tt]
ipNetwork: 139.199.1 ipNetmask: 255.255.0.0 l:
ipNetwork: ipNetmask: 255.255.0.0 l: SJ
ipNetwork: 139.199.10 ipNetmask: 255.255.0.0 l: AVAILABLE
ipNetwork: 139.199.100 ipNetmask: 255.255.0.0 l: AVAILABLE
ipNetwork: 139.99.101 ipNetmask: 255.255.0.0 l: BJ
ipNetwork: 139.99.102 ipNetmask: 255.255.0.0 l: AVAILABLE
ipNetwork: 139.99.4 ipNetmask: l: NJ
[/tt]
 
Hi Futurelet,

Yes, that helped too, thanks for point out another way, it 's good for me to learn it from you guys. Great!

if allow, may I bring up another quick question regarding upper/lower case letters,

I have the line in the file

network-SJ7
Network-sj7

I just want to appear it for once in lower case regardless of the original cases, is there a simple way to do so?

Best regards!

 
Code:
awk 'BEGIN{print tolower("Network-sj7")}'
produces
[tt]
network-sj7
[/tt]
 
Hi futurelet,

thanks! I meant I have many this type of upper/lower cases in one existing file, I guess I could use the way you suggested to work out something to let it go through the whole file and filter this type of cases issues.

Regards!
 
Hi PH,

Just FYI, it was me ... did not describe it very clear and the sample input file that I provided was in that way. In my real file ... (I just notice that sorry), some of the l: lines are not just with one field i.e

l: SJ Network
ipNetwork: 139.100.5
ipNetmask: 255.255.255.0

and with your script the output would be

ipNetwork: 139.100.5 ipNetmask: 255.255.255.0 l: SJ

no "Network" word. Just FYI.

Regards!

 
Yet another try (typed, untested):
^l:/{$1="";l=$0;next}
/^ipNetwork:/{w=$2;next}
/^ipNetmask:/{m=$2;next}
!NF && NR>1{printf "ipNetwork: %s ipNetmask: %s l:%s\n",w,m,l;w=m=l=""}
END{if(length(w m l))printf "ipNetwork: %s ipNetmask: %s l:%s\n",w,m,l}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top