Solved

Multi line to single line - awk , sed or perl

Posted on 2011-03-14
6
787 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
[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
  • 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 500 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

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!

Question has a verified solution.

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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Six Sigma Control Plans

726 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