sed how to delete first 17 lines and last 8 lines in a file

10,332

Solution 1

awk -v nr="$(wc -l < file)" 'NR>17 && NR<(nr-8)' file

Solution 2

head and tail are better for the job than sed or awk.

tail -n+18 file | head -n-8 > newfile

Solution 3

All awk:

awk 'NR>y+x{print A[NR%y]} {A[NR%y]=$0}' x=17 y=8 file
Share:
10,332
Deano
Author by

Deano

Updated on July 21, 2022

Comments

  • Deano
    Deano over 1 year

    I have a big file 150GB CSV file and I would like to remove the first 17 lines and the last 8 lines. I have tried the following but seems that's not working right

    sed -i -n -e :a -e '1,8!{P;N;D;};N;ba' 
    

    and

    sed -i '1,17d' 
    

    I wonder if someone can help with sed or awk, one liner will be great?

  • mtk
    mtk about 11 years
    This seems to be errorneous, as the tail will operate on the output of head, resulting in wrong offset of rows being counted.