Solved

Multi line to single line - awk , sed  with empty line

Posted on 2011-03-15
18
456 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:pierre-alex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
18 Comments
 
LVL 16

Expert Comment

by:medvedd
ID: 35138511
sed -e '1{$p;x;d;}'  -e '/^Device/!{H;$!d;x;s/\n//g;b;}'   -e 'x;s/\n//g;${p;x;}' filename
0
 
LVL 16

Expert Comment

by:medvedd
ID: 35138567
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
0
 

Author Comment

by:pierre-alex
ID: 35138766
Thanks. How do I modify the script so that it looks like:

 cat <filename> | sed .....
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:medvedd
ID: 35138799
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;}'
0
 

Author Comment

by:pierre-alex
ID: 35138884
Sorry

I get the error message:  Too many { 's
0
 

Author Comment

by:pierre-alex
ID: 35138891
Would it be easier to do it with Awk?
0
 
LVL 16

Expert Comment

by:medvedd
ID: 35138931
What shell do you have? No problem with '{' in bash
0
 

Author Comment

by:pierre-alex
ID: 35139251
Using bash too
0
 
LVL 16

Expert Comment

by:medvedd
ID: 35139332
Are you doing copy/paste or retyping sed command? I checked again - it works for me.
0
 

Author Comment

by:pierre-alex
ID: 35139445
Copy and paste. I will try again
0
 

Author Comment

by:pierre-alex
ID: 35139480
This is running on a Solaris box. Would that make a difference?
0
 

Author Comment

by:pierre-alex
ID: 35139570
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
0
 
LVL 16

Expert Comment

by:medvedd
ID: 35139668
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;}'
0
 

Author Comment

by:pierre-alex
ID: 35142869
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


0
 
LVL 16

Expert Comment

by:medvedd
ID: 35148496
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.
0
 

Author Comment

by:pierre-alex
ID: 35149592
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

0
 
LVL 16

Accepted Solution

by:
medvedd earned 500 total points
ID: 35150656
You mean that input file will consist of strings like:

this router is a Alcatel 2550-6550 with 6334/4433 bytes of memory.

cat filename | sed -e 's/^.*with/with/'
0
 

Author Closing Comment

by:pierre-alex
ID: 35151830
Thanks for your help
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question