[comp.unix.questions] novice sed ?

brooks@sundance7.dab.ge.com (Stephen Brooks) (05/04/91)

  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?  I've tried using the N command in sed, but the problem
with it is I don't see how to handle multiple continuation lines without using
multiple N commands.  Is this possible?  What am I missing?

I currently have to do something like this (in sed):
/,$/ N		find "," at end of line, append next line to input pattern
s/\n//		delete embedded <CR>
/,$/ N		do it again (and again and again...)
s/\n//
.
.
.

Thanks!
--
%% Stephen (Steve) M. Brooks       %% brooks@ge-dab.ge.com              %%
%% GE Simulation & Control Systems %% ...!uunet!ge-dab.ge.com!brooks    %%
%% P.O. Box 2825, Rm. 1370         %% ...!uunet!sunny.dab.ge.com!brooks %%
%% Daytona Beach, FL  32115-2825   %% voice: (904) 239-4855             %%

rouben@math16.math.umbc.edu (Rouben Rostamian) (05/04/91)

In article <1991May3.213550.17246@ge-dab.GE.COM> brooks@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

--
Rouben Rostamian                          Telephone: (301) 455-2458
Department of Mathematics and Statistics  e-mail:
University of Maryland Baltimore County   bitnet: rostamian@umbc.bitnet
Baltimore, MD 21228,  U.S.A.              internet: rouben@math9.math.umbc.edu

merlyn@iwarp.intel.com (Randal L. Schwartz) (05/05/91)

In article <1991May3.225202.29639@umbc3.umbc.edu>, rouben@math16 (Rouben Rostamian) writes:
| In article <1991May3.213550.17246@ge-dab.GE.COM> brooks@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@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/

usenet@carssdf.UUCP (John Watson) (05/06/91)

> perl -pe 'while (/,\s*$/) {$_ .= <STDIN>; s/,\s*\n\s*/ /;}' <in >out
> /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\

In this example, don't you have to set $* =1; to get the s/,\s*\n\s*/ to
match.

John Watson

merlyn@iwarp.intel.com (Randal L. Schwartz) (05/06/91)

[I tried mailing.  It bounced.  Where the heck is carssdf?
And yes, further discussion to comp.lang.perl, please...]

In article <304@carssdf.UUCP>, usenet@carssdf (John Watson) writes:
| > perl -pe 'while (/,\s*$/) {$_ .= <STDIN>; s/,\s*\n\s*/ /;}' <in >out
| 
| In this example, don't you have to set $* =1; to get the s/,\s*\n\s*/ to
| match.
| 
| John Watson

No.  $* is only for selecting whether ^ and $ match a
begin/end-of-line within the buffer.  \n will always match itself, as
will \s match newline (and the other whitespace).  The \n is in there
to ensure that I kill the *right* comma and whitespace (which the sed
solution doesn't guarantee).

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