Solved

Multi line to single line - awk , sed or perl

Posted on 2011-03-14
6
779 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 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 

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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux Scripting 3 124
Bash Script to Analyze Oracle Schemas 11 108
Export Variables in Perl 3 70
REGEX HELP 11 43
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,…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
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…

832 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