Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove lines

Status
Not open for further replies.

rhnaeco

Technical User
Aug 11, 2005
45
I'm sure this has been answered before but i can only find answers for removing blank lines.

I would like to remove $n number of lines from the end of a file and $n number of lines from the beginning of the same file.

i.e remove 5 lines from the beginning and 2 from the end of the file.

I don't mind an answer in SED either.


thanks in advance

rhnaeco
 
This uses a circular buffer array to store the previous lines so it can delay printing them out, so that when it reaches the end it hasn't printed the lines you want to skip.

Code:
awk -v skipstart=5 -v skipend=2 '
    NR>skipstart { 
        a[NR%(skipend+1)]=$0 
    } 
    NR>(skipstart+skipend) { 
        print a[(NR-skipend)%(skipend+1)] 
    }
' inputfile > outputfile

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top