print every nth line into a row using gawk

Piece of cake: cat test.txt | awk 'NR % 10 == 1'


It's not (g)awk, but it'll work:

cat myfile | grep ^[[:digit:]]*0[[:blank:]] should do the trick.


With sed, you can do a lot of variations on this quite easily with the first~step command. For instance:

# Odd lines
sed -n 1~2p file
# Every tenth line (10, 20, 30, ...)
sed -n 10~10p file
# Every tenth line (1, 11, 21, ...)
sed -n 1~10p file
# First plus every tenth (1, 10, 20, 30, ...)
sed -n -e 1p -e 10~10p file

To print every second line, starting with the first:

awk 'NR%2==1' file.txt

To print every tenth line, starting with the tenth line:

awk 'NR%10==0' file.txt

To use this in a script, add the following to a file called script.awk:

BEGIN {
    print "Processing file"
}

NR%10==0

END {
    print "Finished processing"
}

Then execute:

awk -f script.awk file.txt