54

Is there a (POSIX command line) way to print all of a file EXCEPT the last n lines? Use case being, I will have multiple files of unknown size, all of which contain a boilerplate footer of a known size, which I want to remove. I was wondering if there is already a utility that does this before writing it myself.

9 Answers 9

56

Most versions of head(1) - GNU derived, in particular, but not BSD derived - have a feature to do this. It will show the top of the file except the end if you use a negative number for the number of lines to print.

Like so:

head -n -10 textfile
4
  • 10
    In POSIX, though, -n must be a positive integer.
    – choroba
    Aug 6, 2012 at 19:53
  • 2
    True enough for strict POSIX. I should have been clearer with my "most". If you're going for a more quick and dirty POSIX method, I would assume wc -l plus some math would get you a value to pass to head
    – Adam B
    Aug 6, 2012 at 20:06
  • most implementations of head(1) lack this feature. Only implementation I know of has this non-portable code. May 7, 2014 at 21:23
  • 7
    In Mac OS X use ghead (installed via brew install coreutils)
    – jchook
    May 17, 2016 at 20:38
16

Probably less efficient than the "wc" + "do the math" + "tail" method, but easier to look at:

tail -r file.txt | tail +NUM | tail -r

Where NUM is one more than the number of ending lines you want to remove, e.g. +11 will print all but the last 10 lines. This works on BSD which does not support the head -n -NUM syntax.

3
  • I think you mean... tac | tail -n+5 | tac
    – anthony
    Jan 10, 2020 at 7:38
  • 1
    @anthony - I needed a solution that also worked on BSD - there is no "tac" on BSD, you have to use "tail -r".
    – user9645
    Jan 16, 2020 at 15:29
  • So neither of this solutions is portable/POSIX-compatible.
    – saulius2
    Dec 16, 2022 at 20:34
8

The head utility is your friend.

From the man page of head:

-n, --lines=[-]K
     print the first K lines instead of the first 10;
       with the leading `-', print all but the last K lines of each file
2
  • 6
    In POSIX, though, -n must be a positive integer.
    – choroba
    Aug 6, 2012 at 19:53
  • 7
    Unfortunately on BSD derived (ie OSX) systems negative values are not valid arguments to -n
    – Tossrock
    Aug 6, 2012 at 20:07
5

There's no standard commands to do that, but you can use awk or sed to fill a buffer of N lines, and print from the head once it's full. E.g. with awk:

awk -v n=5 '{if(NR>n) print a[NR%n]; a[NR%n]=$0}' file
4
cat <filename> | head -n -10 # Everything except last 10 lines of a file
cat <filename> | tail -n +10 # Everything except 1st 10 lines of a file
1

If the footer starts with a consistent line that doesn't appear elsewhere, you can use sed:

sed '/FIRST_LINE_OF_FOOTER/q' filename

That prints the first line of the footer; if you want to avoid that:

sed -n '/FIRST_LINE_OF_FOOTER/q;p' filename

This could be more robust than counting lines if the size of the footer changes in the future. (Or it could be less robust if the first line changes.)

Another option, if your system's head command doesn't support head -n -10, is to precompute the number of lines you want to show. The following depends on bash-specific syntax:

lines=$(wc -l < filename) ; (( lines -= 10 )) ; head -$lines filename

Note that the head -NUMBER syntax is supported by some versions of head for backward compatibility; POSIX only permits the head -n NUMBER form. POSIX also only permits the argument to -n to be a positive decimal integer; head -n 0 isn't necessarily a no-op.

A POSIX-compliant solution is:

lines=$(wc -l < filename) ; lines=$(($lines - 10)) ; head -n $lines filename

If you need to deal with ancient pre-POSIX shells, you might consider this:

lines=`wc -l < filename` ; lines=`expr $lines - 10` ; head -n $lines filename

Any of these might do odd things if a file is 10 or fewer lines long.

2
  • POSIX has the saner $(...) syntax in addition to the ancient backticks for command substitution, and it also has the $((...)) arithmetic expansion. So no need to go ancient with backticks and expr. POSIX Command Substitution
    – geirha
    Aug 6, 2012 at 21:36
  • 1
    @geirha: Good point, and the question did specifically ask about POSIX. But knowing the ancient syntax might be useful in case there are any pre-POSIX shells still in use. Aug 6, 2012 at 22:02
0

tac file.txt | tail +[n+1] | tac

This answer is similar to user9645's, but it avoids the tail -r command, which is also not a valid option many systems. See, e.g., https://ubuntuforums.org/showthread.php?t=1346596&s=4246c451162feff4e519ef2f5cb1a45f&p=8444785#post8444785 for an example.

Note that the +1 (in the brackets) was needed on the system I tried it on to test, but it may not be required on your system. So, to remove the last line, I had to put 2 in the brackets. This is probably related to the fact that you need to have the last line ending with regular line feed character(s). This, arguably, makes the last line a blank line. If you don't do that, then the tac command will combine the last two lines, so removing the "last" line (or the first to the tail command) will actually remove the last two lines.

My answer should also be the fastest solution of those listed to date for systems lacking the improved version of head. So, I think it is both the most robust and the fastest of all the answers listed.

0
head -n $((`(wc -l < Windows_Terminal.json)`)) Windows_Terminal.json

This will work on Linux and on MacOs, keep in mind Mac does not support a negative value. so This is quite handy.

n.b : replace Windows_Terminal.json with your file name

-2

It is simple. You have to add + to the number of lines that you wanted to avoid.

This example gives to you all the lines except the first 9

tail -n +10 inputfile

(yes, not the first 10...because it counts different...if you want 10, just type tail -n 11 inputfile)

1
  • Not what the user asked for!
    – anthony
    Jan 10, 2020 at 7:43

Your Answer

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

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