alternate lines

Hi,
I'm new to Unix. I want to read the all the lines from a text file and write the alternate lines into another file. Please give me a shell script solution.

file1
-----
one
two
three
four
five
six
seven

newfile(it should contain the alternate lines from the file1)
-------
one
three
five
seven

please let me know a solution

Thanks in advance
Pstanand

You can try this shell script

rm -f fileout
i=1
while read line
do
if [[ $i -eq 1 ]]
then
echo $line >>fileout
i=0
continue
fi
if [[ $i -eq 0 ]]
then
i=$((i+1))
continue
fi
done<$filename

$ cat file1
one
two
three
four
five
six
seven

$ awk 'NR%2 {print > "newfile"}' file1

$ cat newfile
one
three
five
seven

//Jadu

Hi Sanjay,

Thank you very much. It works fine. But when I try Jadu code I got the following error.

awk: syntax error near line 1
awk: bailing out near line 1

Can you people pls tell me why this happening?

Regards
pstanand

Try the following awk and sed scripts

awk 'NR%2' file1 > newfile
sed -n '1,${p;n;}' file1 > newfile

HI fpmurphy,
thanks it is great working fine. Can you please explain me how this works?

Regards
pstanand

sed -n 'p;n' filename -- print the odd line
sed -n 'n;p' filename -- print the even line