Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I use sed to quickly delete lines with specific position as

sed '1d'
sed '5d'

But, what if I want to delete the last line of the file and I don't know the count of lines (I know I can get that using wc and several other tricks).

Currently, using a workaround with head and tailcombined with wc to do so. Any quick twists here?

share|improve this question
    
Which os? Some flavours have interesting options for head and tail... –  Nils Oct 24 '12 at 19:24
    
@Nils I am on 'Red Hat Enterprise Linux Server release 5.5 (Tikanga)'. Can you point for the options for head and tail you know of on different flavours? –  mtk Nov 19 '12 at 6:39
    
I did. See below. –  Nils Nov 23 '12 at 20:51
add comment

4 Answers

up vote 23 down vote accepted

in sed $ is the last line so to delete the last line:

sed '$d' <file>
share|improve this answer
add comment

$ for the last line:

sed '$d' file
share|improve this answer
3  
beat you by 30 seconds :P –  h3rrmiller Oct 24 '12 at 14:45
    
Sadly though one has to use more complex techniques to delete, say, the last two lines. –  dubiousjim Oct 24 '12 at 15:13
    
What do you mean? sed '$d' file; sed '$d' file –  Rob Oct 24 '12 at 15:35
1  
@Rob: sed '$d' file doesn't actually modify the file; it just prints out the contents of the file, minus the last line. So sed '$d' file; sed '$d' file will print out the contents of the file twice, minus the last line each time. The delete-the-last-two-lines equivalent of sed '$d' file is sed '$d' file | sed '$d'. –  ruakh Oct 24 '12 at 18:48
1  
Yeah, that's inelegant but of course it works. I would do it in one pass with sed -n '1{h;n;}; $q; x; p;'. I was pouting that we can't just do sed '$-1,$d'. –  dubiousjim Oct 24 '12 at 20:24
show 1 more comment

cat file.txt | head -n -1 > new_file.txt

Beware, it seems, depending on the last line of file.txt (if it ends with EOF, or \n and then EOF), the number of lines in new_file.txt may be the same (as file.txt) after this command (this happens when there is no \n) - in any case, the contents of the last line is deleted.

Also, note that you must use a second (intermediate) file. If you cat and redirect to the same file, you'll erase it.

share|improve this answer
4  
Where this works, it could also be simply head -n -1 file.txt > new_file.txt. Note though that negative counts on head -n is only available for some implementations of head. For example, it doesn't work for FreeBSD's or for the BusyBox version of head. –  dubiousjim Oct 25 '12 at 9:50
1  
@dubiousjim: Agreed, head -n -1 file.txt > new_file.txt is better. It works the same way with respect to my two comments (same number of lines, and, erase on same file). As for the negative argument, such differences occur from time to time between Unixes, and they are often frustrating because you expect it to be the same command entirely (and why wouldn't you - same name and purpose!) -- anyhow, good point. (I use Debian.) –  Emanuel Berg Oct 25 '12 at 17:38
add comment

head --lines=-1. I first stumbled across this possibility in the man-page for head on a SLES11SP2-system (coreutils-8.12-6.23.1)

tail and head are part of the coreutils-rpm (at least for rpm-based-systems).

According to the changelog of coreutils, this syntax is supported since coreutils-version 5.0.1

Bad news: According to the RH5-man-page this option is not described

Good news: It works with RH5 (so in your case: it works - at least with a current version of RH5).

rpm -q coreutils shows me (on CentOS 5.8): coreutils-5.97-34.el5_8.1

I am not sure if RH5.5. already has the coreutils-version that supports it. But 5.5 has EoLed anyway.

share|improve this answer
    
+1. it work's perfectly on RH5.5. Thanks. –  mtk Nov 28 '12 at 6:23
add comment

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.