regexp..prepending a line globally (Vi)

79 views
Skip to first unread message

Randal Schwartz

unread,
Jul 2, 1990, 12:49:49 PM7/2/90
to
In article <17...@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 |
| mer...@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/

Larry Wall

unread,
Jul 2, 1990, 2:47:23 PM7/2/90
to
In article <70...@star.cs.vu.nl> ma...@cs.vu.nl (Maarten Litmaath) writes:
: In article <17...@island.uu.net>,
: dan...@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
lw...@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 de Wit

unread,
Jul 3, 1990, 11:11:30 AM7/3/90
to
In article <85...@jpl-devvax.JPL.NASA.GOV> lw...@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:
|In article <70...@star.cs.vu.nl> ma...@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.

Larry Wall

unread,
Jul 3, 1990, 4:42:21 PM7/3/90
to
In article <8...@ehviea.ine.philips.nl> l...@ehviea.UUCP (Leo de Wit) writes:
: In article <85...@jpl-devvax.JPL.NASA.GOV> lw...@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

Reply all
Reply to author
Forward
0 new messages