[comp.lang.perl] regexp..prepending a line globally

merlyn@iwarp.intel.com (Randal Schwartz) (07/02/90)

In article <1772@island.uu.net>, daniel@island (Daniel Smith - OPD Gang) writes:
| 
| 	All my years in vi and this one stumps me!  I wanted
| to make a real quick shell script, taken from the output
| of an ls-lR on uunet.  So, I get these lines:
| 
| /usr/spool/ftp/comp.sources.unix/volume22/nn6.4:
| part01.Z
| part02.Z
| part03.Z
| part04.Z
| .
| .
| . etc...
| 
| 	So far so good.  Now what I wanted to do is take the first
| line (with the path) and prepend it to every line with a regexp.

You can't do it in vi without resorting to some macro trickery.  (Of
course, the vi hackers will now come out of the woodwork to prove me
wrong... after all, this *is* Usenet. :-)

In Perl, it'd be:

perl -ne 'if (/^(.*):$/) { $pre = $1; } else { print "$pre/$_"; }'

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: "Welcome to Portland, Oregon, home of the California Raisins!"=/

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (07/03/90)

In article <7047@star.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes:
: In article <1772@island.uu.net>,
: 	daniel@island.uu.net (Daniel Smith - OPD Gang) writes:
: )...
: )/usr/spool/ftp/comp.sources.unix/volume22/nn6.4:
: )part01.Z
: )part02.Z
: )part03.Z
: )part04.Z
: ).
: ).
: ). etc...
: )
: )	So far so good.  Now what I wanted to do is take the first
: )line (with the path) and prepend it to every line with a regexp.  [...]
: 
: Not so trivial, I think.  The following sh/sed solution works.
: Try to figure it out!
: --------------------cut here--------------------
: SED='
: 	/spool/{
: : dir
: 		s/:$/\//
: 		h
: : file
: 		n
: 		/spool/b dir
: 		H
: 		g
: 		s/\n//
: 		p
: 		x
: 		s/\n.*//
: 		x
: 		b file
: 	}
: 	p
: '
: 
: sed -n "$SED" ls-output-file

Which just goes to show ya...

    Real programmers can write assembly code in any language.   :-)  :-)  :-)

For those who believe in the waterbed theory of language complexity,
how 'bout:

    #!/usr/bin/perl -p
    if (s#^(/.*):\n##)
	{ $prefix = $1; }
    else
	{ s#^#$prefix/#; }

Fairly trivial, I think.

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov

P.S.  It's not your fault, Maarten--if you push a waterbed down in one spot,
      it just naturally goes up somewhere else.

leo@ehviea.ine.philips.nl (Leo de Wit) (07/03/90)

In article <8571@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:
|In article <7047@star.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes:
|: 
|: Not so trivial, I think.  The following sh/sed solution works.
|: Try to figure it out!
[about 18 lines of sed script omitted]
|
|Which just goes to show ya...
|
|    Real programmers can write assembly code in any language.   :-)  :-)  :-)
|
|For those who believe in the waterbed theory of language complexity,
|how 'bout:
|
|    #!/usr/bin/perl -p
|    if (s#^(/.*):\n##)
|	{ $prefix = $1; }
|    else
|	{ s#^#$prefix/#; }
|
|Fairly trivial, I think.

OK, Larry, you asked for it :-)

sed '/spool/h
//d
G
s/\(.*\)\n\(.*\):/\2\/\1/' $*

Trivial, indeed.

    Leo.

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (07/04/90)

In article <822@ehviea.ine.philips.nl> leo@ehviea.UUCP (Leo de Wit) writes:
: In article <8571@jpl-devvax.JPL.NASA.GOV> lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:
: |    #!/usr/bin/perl -p
: |    if (s#^(/.*):\n##)
: |	{ $prefix = $1; }
: |    else
: |	{ s#^#$prefix/#; }
: |
: |Fairly trivial, I think.
: 
: OK, Larry, you asked for it :-)
: 
: sed '/spool/h
: //d
: G
: s/\(.*\)\n\(.*\):/\2\/\1/' $*
: 
: Trivial, indeed.

That's fine if you're not dyslexic about slashes and backslashes.

If you're gunnin' for the shortest command, try this one on for size:

perl -ne '/:/||print"$`/$_"' $*

(I'll freely admit that if the contest were to double space a file, sed
would win easily.)

Larry