?
Solved

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

Posted on 2011-03-15
18
Medium Priority
?
457 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1500 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

777 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