pierre-alex
asked on
Multi line to single line - awk , sed with empty line
Hello,
I have the following input:
Device1
Data1a Data1b Data1c
Device2
Device3
Data3a Data3b Data3c
Device4
Data4a Data4b Data4c
I need the following output:
Device1 Data1a Data1b Data1c
Device2 empty
Device3 Data3a Data3b Data3c
Device4 Data4a Data4b Data4c
Can you solve the problem using sed or awk?
Many thanks
PA
I have the following input:
Device1
Data1a Data1b Data1c
Device2
Device3
Data3a Data3b Data3c
Device4
Data4a Data4b Data4c
I need the following output:
Device1 Data1a Data1b Data1c
Device2 empty
Device3 Data3a Data3b Data3c
Device4 Data4a Data4b Data4c
Can you solve the problem using sed or awk?
Many thanks
PA
sed -e '1{$p;x;d;}' -e '/^Device/!{H;$!d;x;s/\n// g;b;}' -e 'x;s/\n//g;${p;x;}' filename
Little corrections:
sed -e '1{$p;x;d;}' -e '/^Device/!{H;$!d;x;s/\n/ /g;b;}' -e 'x;s/\n/ /g;${p;x;}' filename
sed -e '1{$p;x;d;}' -e '/^Device/!{H;$!d;x;s/\n/ /g;b;}' -e 'x;s/\n/ /g;${p;x;}' filename
ASKER
Thanks. How do I modify the script so that it looks like:
cat <filename> | sed .....
cat <filename> | sed .....
Just omit filename - sed will accept standart input:
cat filename | sed -e '1{$p;x;d;}' -e '/^Device/!{H;$!d;x;s/\n/ /g;b;}' -e 'x;s/\n/ /g;${p;x;}'
cat filename | sed -e '1{$p;x;d;}' -e '/^Device/!{H;$!d;x;s/\n/ /g;b;}' -e 'x;s/\n/ /g;${p;x;}'
ASKER
Sorry
I get the error message: Too many { 's
I get the error message: Too many { 's
ASKER
Would it be easier to do it with Awk?
What shell do you have? No problem with '{' in bash
ASKER
Using bash too
Are you doing copy/paste or retyping sed command? I checked again - it works for me.
ASKER
Copy and paste. I will try again
ASKER
This is running on a Solaris box. Would that make a difference?
ASKER
Here is the output without using cat:
[me@hostname]$ sed -e '1{$p;x;d;}' -e '/^Device/!{H;$!d;x;s/\n/ /g;b;}' -e 'x;s/\n/ /g;${p;x;}' dump1
Too many {'s
[me@hostname]$ sed -e '1{$p;x;d;}' -e '/^Device/!{H;$!d;x;s/\n/ /g;b;}' -e 'x;s/\n/ /g;${p;x;}' dump1
Too many {'s
Sed on Solaris may be different. Try this:
cat filename | sed -e '1{$p;x;d;}' -e '/^Device/bs' -e 'H;$!d;x;s/\n/ /g;b' -e :s -e 'x;s/\n/ /g;${p;x;}'
cat filename | sed -e '1{$p;x;d;}' -e '/^Device/bs' -e 'H;$!d;x;s/\n/ /g;b' -e :s -e 'x;s/\n/ /g;${p;x;}'
ASKER
No error message now but it is not giving the desired output:
========================== ========== ===
_ input file content:
Device1
data1a data1b data1c
Device2
Device3
data3a data3b data3c
_ command :
[me@hostname]$ cat devices | sed -e '1{$p;x;d;}' -e '/^Device/bs' -e 'H;$!d;x;s/\n/ /g;b' -e :s -e 'x;s/\n/ /g;${p;x;}
___ output:
Device1 data1a data1b data1c Device2 Device3 data3a data3b data3c
========================== ========== =====
As indicated in the initial request I need:
Device1 data1a data1b data1c
Device2 empty
Device3 Data3a Data3b Data3c
Note the keyword "empty" to indicate that Device2 did not have any parameters
Also please note the keyword "Device" will actually be string ... so you can't use it as a regular expression in sed
-------------------------- ---------- ---------- ---------- ---------
The basic idea is that the first column is the name of the device, and the columns after that are the parameters.
If there are no parameters for a device then the script should indicates that by putting the keyword "empty"
Example with more "real" data:
london-pe1
3660/5500
birmingham-ce5
basildon-pop5
660/1243
should give me
london-pe1 3660/5500
birmingham-ce5 empty
basildon-pop5 660/1243
==========================
_ input file content:
Device1
data1a data1b data1c
Device2
Device3
data3a data3b data3c
_ command :
[me@hostname]$ cat devices | sed -e '1{$p;x;d;}' -e '/^Device/bs' -e 'H;$!d;x;s/\n/ /g;b' -e :s -e 'x;s/\n/ /g;${p;x;}
___ output:
Device1 data1a data1b data1c Device2 Device3 data3a data3b data3c
==========================
As indicated in the initial request I need:
Device1 data1a data1b data1c
Device2 empty
Device3 Data3a Data3b Data3c
Note the keyword "empty" to indicate that Device2 did not have any parameters
Also please note the keyword "Device" will actually be string ... so you can't use it as a regular expression in sed
--------------------------
The basic idea is that the first column is the name of the device, and the columns after that are the parameters.
If there are no parameters for a device then the script should indicates that by putting the keyword "empty"
Example with more "real" data:
london-pe1
3660/5500
birmingham-ce5
basildon-pop5
660/1243
should give me
london-pe1 3660/5500
birmingham-ce5 empty
basildon-pop5 660/1243
I don't have access to Solaris sed and can't check my with it, sorry.
Latest version of script was working with FreeBSD sed.
Regarding columns - you need some regular expression to distinguish device names from other data.
Latest version of script was working with FreeBSD sed.
Regarding columns - you need some regular expression to distinguish device names from other data.
ASKER
OK. I have a work around.
Assume the following input:
this router is a Alcatel 2550-6550 with 6334/4433 bytes of memory.
I need the following output using sed:
with 6334/4433 bytes of memory.
If we can do this, the job is done.
thanks
Assume the following input:
this router is a Alcatel 2550-6550 with 6334/4433 bytes of memory.
I need the following output using sed:
with 6334/4433 bytes of memory.
If we can do this, the job is done.
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your help