11

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.

6
  • 3
    This is also a 'gratuitous use of cat' error - tail takes a filename as an argument as well. Jan 6, 2009 at 22:45
  • 1
    Harper, i thought about writing that into my answer, but then i saw that it makes it less readable. having the cat at the beginning, then piping through head & tail is more readable than placing the filename as argument to head imho Jan 6, 2009 at 23:02
  • 1
    Is readability a concern for script one-liners? Heck, the accepted sed answer is, IMHO, a lot less readable than tail -n +15 myfile.txt |head -n -9 . Jan 6, 2009 at 23:24
  • are you sure about the +15? this will only remove 14 lines from, as it "starts the display at the" 15th line.
    – user3850
    Jan 7, 2009 at 0:50
  • 1
    @Harper As a matter of context for my particular problem, the cat at the beginning makes the entire script more readable, which is one reason I prefer it. (The previous and next lines after this command need cat.) Jan 7, 2009 at 14:03

7 Answers 7

14

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.

4

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
0
4

Use a negative number of lines with the head command:

cat myfile.txt | head -n  -9

That prints everything except the last 9 lines.

2
  • 9
    OSX head does not support a negative number of lines.
    – drew
    Mar 12, 2015 at 19:17
  • "OSX head does not support a negative number of lines." That's true, but when this answer was written, the question didn't clearly say that it must run on OS X. Apr 9, 2017 at 15:46
2

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

cat myfile.txt | tail -n +15 | head -n -9
2
  • Much as I hate upvoting those with a higher rep :-), this is exactly what I would have said if I'd turned the computer on 5 minutes earlier this morning. +1.
    – paxdiablo
    Jan 6, 2009 at 22:47
  • Doesn't work on Mac. (...but original question didn't state that solution must work on Mac, so it's OK I guess...) Apr 9, 2017 at 15:18
2

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.

0

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

3
  • 1
    head appears to only let me display the first N lines, which is different from trimming the last N lines. (I don't know exactly how many lines I need to see, just that I need to get rid of the last 9.) Jan 6, 2009 at 22:43
  • head outputs some lines from the beginning - with positive argument, it returns n lines, with negative argument, it returns all but (first) n lines. Tail is similar.
    – jpalecek
    Jan 9, 2009 at 13:17
  • On OSX 10.10.5, with BSD head (June 29, 2006 version), head -n does not take a negative argument. Presumably this was the case in 10.5. On CentOS 6.6 with GNU coreutils (I have version 8.4) head -n does take a negative argument Jun 1, 2017 at 16:12
0

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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.