Multi line to single line - awk , sed or perl

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
pierre-alexAsked:
Who is Participating?
 
point_pleasantCommented:
same using sed

cat infile | sed '$!N;s/\n/ /'
0
 
point_pleasantCommented:
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
 
point_pleasantCommented:
awk one liner if you prefer


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

 
pierre-alexAuthor Commented:
Thanks, I am testing your solutions now
0
 
pierre-alexAuthor Commented:
It doest work, Brillant point pleasant !!!

Can you please explain how the sed code work ?
0
 
point_pleasantCommented:
$!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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.