1825759 Members
2203 Online
109687 Solutions
New Discussion

Need some sed magic

 
SOLVED
Go to solution
Francis Noël
Regular Advisor

Need some sed magic

Hello shell gurus !

I am faced with an Oracle generated text file that contains SQL statements.

The file format is :

Statement semicolon newline
Statement semicolon newline
Statement semicolon newline

Due to a limitation of the generator the statements are written to the file in multiple passes, without regard for individual lines between passes. The result is a file that goes

Statement semicolon newline
Statement semicolon newline
Stat newline
ement semicolon newline
Statement semicolon newline
Statement semicolon newline

So I need to parse the file to find newlines not preceded by a semicolon and get rid of them.

I'm afraid this is beyond my current comfort level with the stream editor but I am convinced it can be done. I considered a multiple pass approach of removing all newlines and then substituting all semicolons with semicolon+newline but that is not elegant.

Your help is greatly appreciated !
3 REPLIES 3
Hein van den Heuvel
Honored Contributor
Solution

Re: Need some sed magic



Just try this:

$ perl -pe 'chomp unless /;$/' broken > fixed

Enjoy,
Hein.
Francis Noël
Regular Advisor

Re: Need some sed magic

Ah yes, indeed a case of the right tool for the job. Quite an interesting logical operator :).

I have learned something today, thank you very much !
Dennis Handly
Acclaimed Contributor

Re: Need some sed magic

>I'm afraid this is beyond my current comfort level with the stream editor

It would probably be move obvious using awk.
But here is a sed solution:
sed -e '
: recheck
/;$/!{ ;# no semicolon at the end
N
s/\n// ;# remove embedded newline
b recheck
}
' broken