Operating System - HP-UX
1834936 Members
2164 Online
110071 Solutions
New Discussion

Re: Retrieve odd lines from a file

 
SOLVED
Go to solution
machoq1
Esteemed Contributor

Retrieve odd lines from a file

I have a file and i need to extract just the odd numbered files from it.
How can i do it ?
5 REPLIES 5
Jean-Luc Oudart
Honored Contributor
Solution

Re: Retrieve odd lines from a file

try this :
awk '{if(t==0) {print $0; t=1;} else t=0;}' >

Regards
Jean-Luc
fiat lux
A. Clay Stephenson
Acclaimed Contributor

Re: Retrieve odd lines from a file

Here's one way:

awk '{ if ((NR % 2) == 1) print; else next }' < infile > outfile
If it ain't broke, I can fix that.
Alan Meyer_4
Respected Contributor

Re: Retrieve odd lines from a file

#!/bin/ksh
NUM=0
cat test.dat |while read LINE ;do
((NUM=NUM+1))
[ $NUM%2 -eq 1 ] && print $LINE
done
" I may not be certified, but I am certifiable... "
Marvin Strong
Honored Contributor

Re: Retrieve odd lines from a file

here is a perl solution.

perl -ne 'print if (($. % 2) == 1)' infile
machoq1
Esteemed Contributor

Re: Retrieve odd lines from a file

All of you guys are amazing....!