to all sed hackers - joining lines with sed

to all sed hackers - joining lines with sed

Post by armin walla » Fri, 13 Jun 2003 04:25:58



hi group!

i have a huge text file with lines that llok like that:

123456     blahblahbla      moreblahblahblah    andsoon
123456     blahblahbla      moreblahblahblah    andsoon
123456     blahblahbla      moreblahblahblah    andsoon
123456     blahblahbla      moreblahblahblah    andsoon
123456     blahblahbla      moreblahblahblah    andsoon
 (of course the lines differ :)

somethimes in that textfiles ther are errors, there are sometimes
newlines there that should not be, like this:

123456     blahblahbla      moreblahblahblah    andsoon
123456     blahblahbla      moreblah
blahblah    andsoon
123456     blahblahbla      moreblahblahblah    andsoon
123456     blahblahbla      moreblahblahblah    andsoon
123456     blahblahbla      moreblahblahblah    andsoon

what i want now is a sed script, that does the following:

if a line starts with a letter or parenthesis, join it with the
preceeding line. i have managed to do so with this example:

cat test| sed -e :a -e '$!N;s/\n[A-Za-z\(\)]//;ta' -e "P;D"

but the problem is that this deletes the first letter of the wrong row
because of
s/\n[A-Za-z\(\)]//
.

so this:

123456     blahblahbla      moreblah
blahblah    andsoon

results in:

123456     blahblahbla      moreblahlahblah    andsoon
                                                                ^^
there should be a "b"

can i somehow preserve that letter or paste what was deleted but
without the preceeding newline?

big thanks in advance!!

 
 
 

to all sed hackers - joining lines with sed

Post by Berend Veldkam » Fri, 13 Jun 2003 16:11:06




Quote:

> cat test| sed -e :a -e '$!N;s/\n[A-Za-z\(\)]//;ta' -e "P;D"

> but the problem is that this deletes the first letter of the wrong row
> because of
> s/\n[A-Za-z\(\)]//
> .

It looks like the following works:
cat test| sed -e :a -e '$!N;s/\n\([A-Za-z()]\)/\1/;ta' -e "P;D"

Berend

 
 
 

to all sed hackers - joining lines with sed

Post by armin walla » Sat, 14 Jun 2003 01:49:10





> > cat test| sed -e :a -e '$!N;s/\n[A-Za-z\(\)]//;ta' -e "P;D"

> > but the problem is that this deletes the first letter of the wrong row
> > because of
> > s/\n[A-Za-z\(\)]//
> > .

> It looks like the following works:
> cat test| sed -e :a -e '$!N;s/\n\([A-Za-z()]\)/\1/;ta' -e "P;D"

> Berend

Yeeee!!
thank you very much, you're a life saver :)
the sed manpage could really use some examples i think

best wishes, armin

 
 
 

1. sed: act on previous line, and join two lines?

There are two actions I frequently wish I could do with sed, but always end
up doing it some other way. Anyone know how to do these? I may use
combinations of tr, sed, cut, and paste right now to do all this, but I'd
rather just use sed.

1. select or print a single line immediately preceding an identified line.
For example, in a text file with FF page breaks, I want every line that
immediately precedes the FF line. Of course, I may also wish to target the
4th line before the FF line, or maybe the 2nd line after the FF line, etc.
(you get the idea). In an old line editor I used to use, I could use the
regexp "/^L/-1" to find the FF line, and then position backwards 1 line.

2. join two lines. For example, in a text file with FF page breaks, I want
to append the FF line to the line immediately preceding it (in vi I can use
the "J" command to join two lines).

--

Internal Systems Coordinator
DataMirror Corporation  (anti-spam email address suffix in use)

2. Help needed to mount CDROM!

3. How to join lines wiht the line number in sed ?

4. SVGAlib & Chips 65545 advanced features

5. sed sed sed

6. How do I decode .shar files?

7. . 2 sed FAQs, sed exes, sed cetras; URLs

8. Finding application versions?

9. SED SED SED...

10. (sed 1q ; sed 2q) : no output from 2nd 'sed'

11. how can I join a range of lines in sed?

12. HELP: join lines with sed .....

13. Sed: How to join lines back