novice sed ?

Randal L. Schwartz merlyn at iwarp.intel.com
Mon May 6 00:07:40 AEST 1991


In article <1991May3.225202.29639 at umbc3.umbc.edu>, rouben at math16 (Rouben Rostamian) writes:
| In article <1991May3.213550.17246 at ge-dab.GE.COM> brooks at sundance7.dab.ge.com (Stephen Brooks) writes:
| >  I have a sed question for you gurus: assume I have a file of the form
| >
| >name1; A1.1 B10.20 C100.300,
| >       D101.25 E202.50,
| >       F300.7
| >name2; Z44.33 Y409.45
| >name3; X777.77 W6.6,
| >       V32.15
| >
| >where a comma (,) represents a continuation character.  I need to "massage"
| >this into something which looks like this:
| >
| >name1; A1.1 B10.20 C100.300 D101.25 E202.50 F300.7
| >name2; Z44.33 Y409.45
| >name3; X777.77 W6.6 V32.15
| >
| >Can I do this in sed? 
| 
| Yes.  Here it is:
| 
| sed -n '
| :loop
| /,$/{N
| bloop
| }
| s/,\n//g
| s/  */ /g
| p'  <inputfile

And in Perl... (see, I waited for the sed solution to be posted this time :-):

perl -pe 'while (/,\s*$/) {$_ .= <STDIN>; s/,\s*\n\s*/ /;}' <in >out

(which means, while you see a comma at the end of the line (ignoring
trailing whitespace), append the next input line, and replace the
comma, optional whitespace, newline, and more optional whitespace with
a single space.)

print "Just another Perl hacker,"
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn at iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/



More information about the Comp.unix.questions mailing list