Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 462
  • Last Modified:

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
0
pierre-alex
Asked:
pierre-alex
  • 10
  • 8
1 Solution
 
medveddCommented:
sed -e '1{$p;x;d;}'  -e '/^Device/!{H;$!d;x;s/\n//g;b;}'   -e 'x;s/\n//g;${p;x;}' filename
0
 
medveddCommented:
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
 
pierre-alexAuthor Commented:
Thanks. How do I modify the script so that it looks like:

 cat <filename> | sed .....
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
medveddCommented:
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
 
pierre-alexAuthor Commented:
Sorry

I get the error message:  Too many { 's
0
 
pierre-alexAuthor Commented:
Would it be easier to do it with Awk?
0
 
medveddCommented:
What shell do you have? No problem with '{' in bash
0
 
pierre-alexAuthor Commented:
Using bash too
0
 
medveddCommented:
Are you doing copy/paste or retyping sed command? I checked again - it works for me.
0
 
pierre-alexAuthor Commented:
Copy and paste. I will try again
0
 
pierre-alexAuthor Commented:
This is running on a Solaris box. Would that make a difference?
0
 
pierre-alexAuthor Commented:
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
 
medveddCommented:
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
 
pierre-alexAuthor Commented:
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
 
medveddCommented:
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
 
pierre-alexAuthor Commented:
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
 
medveddCommented:
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
 
pierre-alexAuthor Commented:
Thanks for your help
0

Featured Post

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.

  • 10
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now