Solved

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

Posted on 2011-03-15
18
450 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…

803 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