Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to print the odd-numbered and even-numbered lines from files.

I found this shell script which makes use of echo.

#!/bin/bash
# Write a shell script that, given a file name as the argument will write
# the even numbered line to a file with name evenfile and odd numbered lines
# in a text file called oddfile.
# -------------------------------------------------------------------------
# Copyright (c) 2001 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------

file=$1
counter=0

eout="evenfile.$$" # even file name
oout="oddfile.$$" # odd file name

if [ $# -eq 0 ]
then
    echo "$(basename $0) file"
    exit 1
fi

if [ ! -f $file ]
then
    echo "$file not a file"
    exit 2
fi

while read line
do
    # find out odd or even line number
    isEvenNo=$( expr $counter % 2 )

    if [ $isEvenNo -ne 0 ]
    then
        # even match
        echo $line >> $eout
    else
        # odd match
        echo $line >> $oout
    fi
    # increase counter by 1
    (( counter ++ ))
done < $file
echo "Even file - $eout"
echo "Odd file - $oout"

But isn't there a way to do it in one line?

Yes, use awk, I read.

Even-numbered lines:

awk 'NR % 2' filename

odd-numbered lines:

awk 'NR % 2 == 1' filename

But it doesn't work for me. Both produce the same output, according to diff. When compared to the original file, they are both indeed half as long, and they both contain the odd-numbered lines. Am I doing something wrong?

share|improve this question
2  
The first one should be NR % 2 == 0, otherwise it is equivalent to the second one. –  enzotib Dec 13 '11 at 11:49
    
There seem to be several documents online (including this one) that show up at the top of a search that state that NR % 2 gives you the even numbered lines, which is not correct, it gives you the odd because 1 % 2 = 1 = true, 2 % 2 = 0 = false. –  deltaray Jan 24 '12 at 16:29
add comment

4 Answers 4

up vote 4 down vote accepted

As you asked “in one line”:

awk '{print>sprintf("%sfile.%d",NR%2?"odd":"even",PROCINFO["pid"])}' filename

Note that most of the code is due to your fancy output filename choice. Otherwise the following code would be enough to put odd lines in “line-1” and even lines in “line-0”:

awk '{print>"line-"NR%2}' filename
share|improve this answer
add comment

That's easy:

 sed -n 2~2p filename

will print even-numbered lines from filename

sed -n 1~2p filename

will print odd-numbered lines.

share|improve this answer
    
+1, for not using AWK extraneously. Not POSIX sed, but it's still solid method. –  TechZilla Nov 1 '12 at 14:33
    
@TechZilla I don't understand "using AWK extraneously" - awk is POSIX as well. –  jw013 Nov 1 '12 at 15:31
1  
@jw013: Nothing is wrong with awk, personally I use it very often. I never said anything was 'Not POSIX' about awk, I was referring to the answer's sed options. Specifically the ~ operator, it's a GNU extension, which is still acceptable for many people. Regarding 'using AWK extraneously`, I personally believe using awk for this simple task is overkill. So the +1 was for for completing the task with sed, a lighter utility than awk. –  TechZilla Nov 1 '12 at 15:55
add comment

I prefer to be POSIX compatible, whenever possible, so I thought I'd post this alternative method. I often use these to mangle text, before xargs pipelines.

Print Even Numbered Lines,

sed -n 'n;p'

Print Odd Numbered Lines,

sed -n 'p;n'

Although I often use awk, it's overkill for this type of task.

share|improve this answer
    
+1 this is a brilliant one! The simplicity... –  peterph Feb 13 at 14:20
add comment

Try this:

awk '{if(NR%2){print $0 > "odd.file"}else{print $0 > "even.file"}}' filename
share|improve this answer
    
Are you sure about outputting the record numbers? –  manatwork Dec 13 '11 at 12:17
    
sorry about that, I modified it to output the whole lines. –  renma Dec 13 '11 at 12:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.