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!


 
A starting point:
awk 'BEGIN{ORS=" "}{print}END{printf "\n"}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PH,

it does help, and I could not help to ask more, in fact my file is like this

l: AVAILABLE
ipNetwork: 139.139.0.0
ipNetmask: 255.255.0.0

l: SJ
ipNetwork: 139.140.0.0
ipNetmask: 255.255.0.0

... ...

I want to make it look like below with the specific order in each line

ipNetwork:139.139.0.0 ipNetmask:255.255.0.0 l: AVAILABLE
ipNetwork:139.140.0.0 ipNetmask:255.255.0.0 l: SJ
... ...

I don't know much about awk yet (start learning), could you give some comments again? thanks.

Regards!
 
awk '
/^l:/{f=$0;next}
!NF{print f;next}
{printf "%s ",$0}
END{if(NF)print f}
' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PH,

Thanks for the input, when I tried, said

awk: syntax error near line 1
awk: bailing out near line 1

maybe I need to learn more to explain this.

Merci.

Regards!
 
You may try nawk instead of awk.
You may have quoting problem (single quote vs double quote)
Have you tried to copy/paste the awk program ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PH,

Yes I tried copy/paste, I also tried double quotation mark but this time as you suggested, when tried to use nawk, it works just perfect and great!

However I met another problem, it's not the script, it's my text file, most of the lines are in above order that I gave out, but some of the lines are in different order

ipNetmask: xxx.xxx.xxx.xxx
ipNetwork: xxx.xxx.xxx.xxx
l: xxx

or some of the paragraph does not have l: xxx line,

ipNetwork: xxx.xxx.xxx.xxx
ipNetmask: xxx.xxx.xxx.xxx

or l: xxx is the first line etc.

as ran your scripts, most of the lines are in the order I wanted except a few lines mentioned above, anyway they have one common thing is the l: xxx is always the last pattern, so I m wondering if there is a way to force such an order in the output file even the input file is in different order.

Appreciate your time and your great help.

Regards!
 
The Awk way:
Code:
BEGIN { RS = ""; FS=" |\n"; OFS="  " }
{ print $3$4, $5$6, $1 " " $2 }
Let me know whether or not this helps.

If you have nawk, use it instead of awk because on some systems awk is very old and lacks many useful features. Under Solaris, use /usr/xpg4/bin/awk.

For an introduction to Awk, see faq271-5564.

 
Here is another way using a shell script

Code:
#!/bin/bash

if [ "$#" -ne 1 ] ; then
	echo "USAGE: $0 file"
	exit 9
fi

MASK=$(grep "ipNetmask" "$1") 
if [ "$?" -ne 0 ] ; then
	echo "In file does not have ipNetmask"
	exit 1
fi

WORK=$(grep "ipNetwork" "$1")
if [ "$?" -ne 0 ] ; then
	echo "In file does not have ipNetwork"
	exit 2
fi

LOC=$(grep "l:" "$1")

echo "$WORK $MASK $LOC"
 
some of the lines are in different order
Code:
BEGIN { RS = ORS = ""; FS="\n"; OFS="  " }
{ space = last = ""
  for (i=1; i<=NF; i++)
  { sub( /\n/, "", $i )
    if ($i ~ /^.: / )
      last = "  " $i
    else
    { print space $i
      space = "  "
    }
  }
  print last "\n"
}
 
Hi Guys,

you are all very generous, here is my feedback, I wish I knew more and wouldn't bother you this much.

for futurelet 's scripts
the first one works for most of the lines but not for those with different order, and it generated other output as well (just for individual cases, the characters from the end of above line appeared in the front of next line)
the second scripts, I tried nawk but it complained
nawk: syntax error at source line 1
tried with different quotation marks as well.

for Skilift's scripts
it will work for only one line but for multiple lines it will organized the output in this way ... all lines with ipNetwork and then all lines with ipNetmask and all lines with Locations.

Just a feedback to let you know. Thanks again.

Regards!


 
Hi futurelet,

my mistake, the 2nd script works but still could not deal with those lines in different orders, another questions is some of the input lines/sections do not have the l: line or ipNetwork line while in the output file is there a way to add it and keep the whole line as ipNetwork:ipNetmask:l thanks.

Regards!
 
Hi,

the sample file is like below

ipNetwork: 139.199.1
ipNetmask: 255.255.0.0

l: SJ
ipNetmask: 255.255.0.0

ipNetwork: 139.199.10
ipNetmask: 255.255.0.0
l: AVAILABLE

ipNetwork: 139.199.100
l: AVAILABLE
ipNetmask: 255.255.0.0

l: BJ
ipNetwork: 139.99.101
ipNetmask: 255.255.0.0

ipNetmask: 255.255.0.0
l: AVAILABLE
ipNetwork: 139.99.102

l: NJ
ipNetwork: 139.99.4


want to make it like following


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

with PH's script output is

ipNetwork: 139.199.1 ipNetmask: 255.255.0.0
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
ipNetmask: 255.255.0.0 ipNetwork: 139.99.102 l: AVAILABLE
ipNetwork: 139.99.4

with futurelet's script output is

ipNetwork: 139.199.1 ipNetmask: 255.255.0.0
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
ipNetmask: 255.255.0.0 ipNetwork: 139.99.102 l: AVAILABLE
ipNetwork: 139.99.4 l: NJ

with Skilife 's script output is

ipNetwork: 139.199.1
ipNetwork: 139.199.10
ipNetwork: 139.199.100
ipNetwork: 139.99.101
ipNetwork: 139.99.102
ipNetwork: 139.99.4 ipNetmask: 255.255.0.0
ipNetmask: 255.255.0.0
ipNetmask: 255.255.0.0
ipNetmask: 255.255.0.0
ipNetmask: 255.255.0.0
ipNetmask: 255.255.0.0 l: SJ
l: AVAILABLE
l: AVAILABLE
l: BJ
l: AVAILABLE
l: NJ

just FYI. Thanks.


 
The following works for me:
/^l:/{l=$0;next}
/^ipNetwork:/{w=$2;next}
/^ipNetmask:/{m=$2;next}
!NF && NR>1{printf "ipNetwork: %s ipNetmask: %s %s\n",w,m,l;w=m=l=""}
END{if(NF && length(w m l))printf "ipNetwork: %s ipNetmask: %s %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
 
Hi PH,

Thanks, when I did that on the sample file, please see below first line, if there is no l: line originally, there will be no l: appended. Tried it on the acctual file with the same output. Others works perfect.

ipNetwork: 139.199.1 ipNetmask: 255.255.0.0
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

Merci mille fois!

Regards!
 
Hi PH,

To follow above ... and the last line was missed.

should be

ipNetwork: 139.99.4 ipNetmask: l: NJ

Thanks and regards!


 
^l:/{l=$2;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 %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
 
Hi PH,

Thanks, only the last line's l: is missing.

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: NJ

Regards!
 
Hi PH,

added, thanks for you effort, I will study from your script.

Thanks a lot!

/^l:/{l=$2;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}

Best regards!
 
Oops, my bad:
^l:/{l=$2;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}

The correct way to avoid this kind of bug is to write functions instead of (baddly)duplicated lines of code

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