R.Singh@massey.ac.nz (R. Singh) (06/26/91)
Can you say, on every even line number, substitute something for something. Or on every odd line number, execute a command. Thanx Raminder $ingh
tchrist@convex.COM (Tom Christiansen) (06/26/91)
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."
hansm@cs.kun.nl (Hans Mulder) (06/26/91)
In <1991Jun26.025626.29367@massey.ac.nz> R.Singh@massey.ac.nz (R. Singh) writes: >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 hansm@cs.kun.nl
carlson@mrx.webo.dg.com (James Carlson) (06/27/91)
> :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: carlson@mrx.webo.dg.com -or- James_Carlson@dgc.mceo.dg.com
.//.