Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tail inverse / printing everything except the last n lines?

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.

like image 318
Tossrock Avatar asked Aug 06 '12 19:08

Tossrock


People also ask

How do I see the last 10 lines of a file in Linux?

tail [OPTION]... [ Tail is a command which prints the last few number of lines (10 lines by default) of a certain file, then terminates. Example 1: By default “tail” prints the last 10 lines of a file, then exits.

What is the opposite of tail in Linux?

head is a basic opposite of tail in relation to what part of the file operations are performed on. By default, head will read the first 10 lines of a file.

How do you find the last line of a file in Linux?

To display the last part of the file, we use the tail command in the Linux system. The tail command is used to display the end of a text file or piped data in the Linux operating system. By default, it displays the last 10 lines of its input to the standard output. It is also complementary of the head command.

How do you print the last line of a file in Unix using SED?

In sed, $ indicates the last line, and $p tells to print(p) the last line($) only. 4. Another option in sed is to delete(d) all the lines other than(!) the last line($) which in turn prints only the last line.


1 Answers

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 
like image 143
Adam B Avatar answered Sep 22 '22 18:09

Adam B