Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

3 Answers 3

up vote 9 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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