Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Joining lines

Status
Not open for further replies.

mrn

MIS
Apr 27, 2001
3,993
GB
Hello,

I have the following in a file (not the actual data)

12345
abcdefghij
klmnopqrstuvwxyz;
12345
abcdefghij
klmnopqrstuvwxyz;
12345
abcdefghij
klmnopqrstuvwxyz;

I wish to join the line ending in ; to the previous line how can I do this?

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant.
 
Just a thought
Using your favourite language

while read line
strip trailing lf
if not eding in ';'
print lf
print line

in perl
Code:
while (<>)
  {
  chomp;
  ! /;$/ and print "\n";
  print;
  }
print "\n";

I'm sure the perl gurus can providfe more compact code!
 
In awk...
Code:
awk '{printf}/;$/{printf RS}' file1
 
something like this ?
awk '
/;$/{for(i=0;i<n-1;++i)print a;print a$0;n=0;next}
{a[n++]=$0}
' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
On SCO, the "paste" command has a special flag (-s) that provides the functionality you seek.

man paste

but the following should give you exactly what you requested:

Code:
paste -s -d "\0\0\n" input-file > output-file

&quot;Code what you mean,
and mean what you code!
But by all means post your code!&quot;

Razalas
 
Ygor

Nearly there but it also does the 12345

12345 abcdefghijklmnopqrstuvwxyz;


Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant.
 
Try this:

Code:
awk '
BEGIN{
     prevline="";
     }

/;$/{
    print prevline $0;
    prevline=""
    }

(prevline != ""){
    print prevline
    }

!/;$/{
    prevline=$0
    }
END{
    if(prevline != ""){
        print prevline
        }
    }
' inputfile

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Thanks Guys

I've got enough info to do the job now,

I was trying

rev File.Name | sed -e :a -e '$!N;s/\n;/;/;ta' -e 'P;D' | rev

It nearly worked but put the lines the wrong way round....

12345
klmnopqrstuvwxyz;abcdefghij


Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant.
 
Sorry Mike, I missunderstood you the first time. Try this instead...

awk '{if(!/;$/)print b;else printf b;b=$0}END{print b}' file1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top