Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can get a file, except the last (for instance) 2 lines, with standard or GNU shell tools? (bash, tail, cat and so on.)

share|improve this question

migrated from stackoverflow.com Jan 16 '13 at 10:53

This question came from our site for professional and enthusiast programmers.

    
related: stackoverflow.com/questions/604864/… – Prof. Falken Jan 16 '13 at 9:11
    
Anything special on those last lines, so you can use grep(1)? – vonbrand Jan 23 '13 at 18:21
    
@vonbrand, no, nothing known. – Prof. Falken Jan 24 '13 at 8:22
up vote 10 down vote accepted

Use the head command from coreutils:

head -n -2

See info head for more.

share|improve this answer
1  
info instead of man, daring, my good fellow. ;) +1 – Prof. Falken Jan 16 '13 at 9:17
    
@AmigableClarkKant: the coreutils manpages are not as well maintained as the texinfo pages, not that the head documentation differs much between the two though. – Thor Jan 16 '13 at 9:34

You simply want.

head -n-2 file

Read man head for more information.

share|improve this answer

This command gives the whole $file except the last $except number of lines:

length=$(wc -l $file); head -n $((${length%% *}-except)) $file

(No checking whether $except is larger than the total number of lines of $file...)

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .