Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's an easy way to trim N lines from the tail of a file (without the use of 'head')?

Tags:

People also ask

Which command will be used to delete N number of lines?

To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.


Say I've got a bunch of files that are all >100 lines long. I'd like to trim off the top 14 lines and the bottom 9 lines, leaving only the lines in the middle. This command will trim off the top fourteen:

cat myfile.txt | tail -n +15

Is there another command I can pipe through to trim off the bottom 9 without explicitly passing the length of the file?

Edited to add: My version of head (Mac OS 10.5) doesn't accept a negative number of lines as a parameter.