Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Multi line to single line - awk , sed or perl

Posted on 2011-03-14
6
Medium Priority
?
802 Views
Last Modified: 2012-05-11
I need to transform the following text:

Device1
data1a data1b
Device2
data2a data2b
Device3
data3a data3b

Into:

Device1 data1a data1b
Device2 data2a data2b
Device3 data3a data3b

My preference is to use awk or sed, perl at the last recourse

Can you please have a look?

Thanks

PA
0
Comment
Question by:pierre-alex
  • 4
  • 2
6 Comments
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35129944
awk ' BEGIN { cntr = 1 }
{
  printf("%s ",$0)
  if ( cntr == 2 ) {
    printf("\n")
    cntr = 0
  }
  cntr++
}
END {
  rest = int(cntr)
  if ( rest != 1 ) {
    printf("\n")
  }
}
' infile
~
0
 
LVL 8

Accepted Solution

by:
point_pleasant earned 2000 total points
ID: 35129981
same using sed

cat infile | sed '$!N;s/\n/ /'
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35129996
awk one liner if you prefer


awk '{ if ( ( NR % 2 ) == 0 ) { printf("%s\n",$0) } else { printf("%s ",$0) } }' file
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!

 

Author Comment

by:pierre-alex
ID: 35130012
Thanks, I am testing your solutions now
0
 

Author Comment

by:pierre-alex
ID: 35130053
It doest work, Brillant point pleasant !!!

Can you please explain how the sed code work ?
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35131007
$!N means read in the next line only if it is not the last line in the file. So read in the next line and strip the \n from the first.  This only works for joing two lines though.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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…

877 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