awkcsh

Print line every four lines from an file


I have a file that contains 8000 lines, I want to print lines 1,4,8,12,...,7996.

I tried this code

for j in {1 .. 8000}
do
k= $((4 * $j))
print k 
sed -n $k P test.dat >> test.dat1 
done

but this error appears:

./test.csh: line 3: 4 * {1: syntax error: operand expected (error token is "{1")

what is the problem, how can I do this?


Solution

  • Use awk command:

    awk 'NR%4==1{print}' input.txt
    

    Explanation:

    NR % 4 == 1 { # for every input line, which line number (NR) modulo 4 is 1
        print $0; # print the line
    }