Christopher Ashworth
Christopher Ashworth

Reputation: 4334

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

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.

Upvotes: 11

Views: 9198

Answers (7)

user3850
user3850

Reputation:

This will work on OS X and might be a bit more easily understandable than the sed example:

< myfile.txt tail -n +15 | tail -r | tail -n +10 | tail -r

Of course, if you can get your hands on GNU's version of head, it can be done even more elegantly:

< myfile.txt tail -n +15 | head -n -9

Be aware the tail starts at the nth line while head skips n lines of the input.

Upvotes: 14

user25148
user25148

Reputation:

This should also work, and does things in a single process:

seq 15 | 
awk -v N=5 '
  { lines[NR % N] = $0 } 
  END { i = NR-N+1; if (i<0) i=0; for (; i <= NR; ++i) print lines[i % N] }'

(The seq is just an easy way to produce some test data.)

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 754520

If you can recognize the last 9 lines by a distinctive pattern for the first of those lines, then a simple sed command would do the trick:

sed -e '1,15d' -e '/distinctive-pattern/,$d' $file

If you need a pure numeric offset from the bottom, standard (as opposed to GNU) sed won't help, but ed would:

ed $file <<'!'
1,15d
$-8,$d
w
q
!

This overwrites the original files. You'd have to script where the file is written to if you wanted to avoid that.

Upvotes: 2

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507115

What jbourque said is completely right. He just wasn't too wordy about it:

cat myfile.txt | tail -n +15 | head -n -9

Upvotes: 2

Paige Ruten
Paige Ruten

Reputation: 176743

Use a negative number of lines with the head command:

cat myfile.txt | head -n  -9

That prints everything except the last 9 lines.

Upvotes: 4

Rob Hruska
Rob Hruska

Reputation: 120346

You could use sed:

sed -n -e :a -e '1,9!{P;N;D;};N;ba' myfile.txt

You can also use sed for the first 15:

sed '1,15d' myfile.txt

Upvotes: 4

Jeremy Bourque
Jeremy Bourque

Reputation: 3543

The head command should do what you want. It woks just like tail but from the other end of the file.

Upvotes: 0

Related Questions