Search code examples
filesedawkgreplines

grep Trim txt file by certain line number


I have a txt file containing, let's say, 1000 lines. I would like to trim it obtaining a file with 100 lines, composed by lines 0, 10, 20, 30, etc of the original file.

Is that possible with grep or something? thanks


Solution

  • it could be easily done by awk/sed one-liner:

    awk

    awk '!(NR%10)' file
    

    sed

    sed -n '0~10p' file
    

    or

    sed '0~10!d` file
    

    see below example: (sed one liner will give same output)

    print the first 10 lines:

    kent$  seq 1000|awk '!(NR%10)'|head -10
    10
    20
    30
    40
    50
    60
    70
    80
    90
    100
    

    total lines:

    kent$  seq 1000|awk '!(NR%10)'|wc -l   
    100