Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Substituting on even or odd line numbers in VI?

643 views
Skip to first unread message

R. Singh

unread,
Jun 25, 1991, 10:56:26 PM6/25/91
to
Can you say, on every even line number, substitute something for something.
Or on every odd line number, execute a command.

Thanx
Raminder $ingh

Tom Christiansen

unread,
Jun 26, 1991, 7:30:53 AM6/26/91
to
From the keyboard of R.S...@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 tch...@convex.com convex!tchrist
"So much mail, so little time."

Hans Mulder

unread,
Jun 26, 1991, 8:51:58 AM6/26/91
to

>Can you say, on every even line number, substitute something for something.

Not directly, but you can use the trick below.

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

Yes, at least for ex-commands. If you say

:g/^/command|+t+|-d

the command gets executed on all odd numbered lines, provided it doesn't move
the cursor off the line. If you want to move the cursor, you could say

:g/^/+t+|-d|-command

but then your command wouldn't be executed on the last line if you had
an odd number of lines in the buffer.

So to substitute something on all even numbered lines, you should say

:2,$g/^/s/foo/bar/g|+t+|-d

--
Have a nice day,

Hans Mulder ha...@cs.kun.nl

James Carlson

unread,
Jun 26, 1991, 3:18:01 PM6/26/91
to
> :2,$g/^/s/foo/bar/g|+t+|-d

Wow! That's as clear as mud! For reference, executing:

select +1
repeat *

In KEDIT (or XEDIT) will select for editing all of the even lines. You can then
execute any commands against this subset (including block mark/copy/move/delete
and global search/replace). To return to viewing and editing the whole file,
enter:

all

(To get 2 out of every three, do "1 select +1" instead. To reverse the
selection (to 1 out of 3 or to the odd lines in the previous example) use:

display 1

Since all that the 'select' command really does is tag each line (default 0)
with a level number, you can switch back and forth between the displayed and
undisplayed lines with a single command. For humor value, you could bind this
to a key ...)

--
Disclaimer: I cannot speak for Data General. You should have guessed that.
Mail to: car...@mrx.webo.dg.com -or- James_...@dgc.mceo.dg.com
.//.

0 new messages