[alt.sources.wanted] every other line of a text file

merlyn@iwarp.intel.com (Randal Schwartz) (06/14/90)

In article <1990Jun13.181047.12420@athena.mit.edu>, lfk@E40-008-8 (Lee F Kolakowski) writes:
| On 13 Jun 90 17:26:27 GMT,
| stevep@dgp.toronto.edu (Steve Portigal) said:
| >   I am looking for a simple Unix way to extract every other line of 
| > a text file.  Can anyone help?  Email please and I'll post one summary.
| If you have awk or gawk it is very simple...
| 
| Try this:
| 
| for lines 0, 2, 4, .....
| 
| gawk '
| {
| 	if (!(NR % 2) {
| 		print $0
| 	}
| }' 
| 
| for lines 1, 3, 5, ...
| 
| gawk '
| {
| 	if (NR % 2) {
| 		print $0
| 	}
| }' 
| 
| These scripts are very simple.  They use the built-in variable NR
| (record number) to count lines.  If the number of lines modulo 2
| equals zero the expression (!(NR % 2)) is true and the line will be
| printed.

and of course, in Perl, it'd be:

perl -ne 'print if $. % 2'

for the odd lines (even if you count beginning at zero :-), and:

perl -ne 'print unless $. % 2'

for the even lines (odd if you count beginning at zero :-).

[note to Perl fanatics... requires 3.18]

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!"=/

tneff@bfmny0.BFM.COM (Tom Neff) (06/14/90)

Aw come on, Tom and Randal.

	perl -ne 'print if $. % 2;'

is for poofters!  Real men use

	perl -pe '$_ = <>;'

:-)
-- 
The real problem with SDI is     %/    Tom Neff
that it doesn't kill anybody.    /%    tneff@bfmny0.BFM.COM