Solved

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

Posted on 2011-03-15
18
444 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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 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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now