🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.editors
Article #2857

Re: Substituting on even or odd line numbers in VI?

#2857
From: Tom Christiansen
Date: Wed, 26 Jun 1991 11:30
47 lines
2063 bytes
From the keyboard of R.Singh@massey.ac.nz (R. Singh):
>Can you say, on every even line number, substitute something for something.

With or without a chain-saw? :-)

:%!perl -pe 's/foo/bar/ unless $. \% 2'

>Or on every odd line number, execute a command.

I'm not sure what you mean by ``execute a command''.  What are you going
to do with the line, supply it as stdin or argv for the command?  Do you
mean to run the line THROUGH a command and replace that line with that
command's output?

Ok, for the sake of display, here's such a program.  It runs "rev"
on the odd lines.  
Of course, you'd really use the perl built-in
called reverse.

    #!/usr/bin/perl -ne
    $| = 1;
    (print,next) unless $. % 2;
    if (open(F,"|-")) {
	print F; close F;
    } else {
	exec 'rev';
    } 

that is...

:%!perl -ne '$|=1;(print,next)unless$. \%2;if(open(F,"|-")){print F;close F;}else{exec "rev";}'

Of course, in this case, you'd really use the perl built-in:

:%!perl -ne 'print $. \% 2 ? $_ : scalar(reverse)'

But maybe you'd want to 'finger -m -s' every other line or something.

The point here is not that you should necessarily use perl for all these
things (although the first is probably a good use) -- everyone has their
own trained seal.  The point is that you this is UNIX, so it's ok (and a
good idea) to put tools together to achieve the desired effect.

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
		"So much mail, so little time."  
Technical Details
Message-ID: <1991Jun26.113053.1439@convex.com>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!convex!usenet
References: <1991Jun26.025626.29367@massey.ac.nz>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!convex!usenet
From: tchrist@convex.COM (Tom Christiansen)
Newsgroups: comp.editors
Subject: Re: Substituting on even or odd line numbers in VI?
Message-ID: <1991Jun26.113053.1439@convex.com>
Date: 26 Jun 91 11:30:53 GMT
References: <1991Jun26.025626.29367@massey.ac.nz>
Sender: usenet@convex.com (news access account)
Reply-To: tchrist@convex.COM (Tom Christiansen)
Organization: CONVEX Software Development, Richardson, TX
Lines: 47
Nntp-Posting-Host: pixel.convex.com