rone: (Default)
entombed in the shrine of zeroes and ones ([personal profile] rone) wrote2008-12-04 11:51 pm
Entry tags:

dear lazyweb

I want to run a command that will give me the contents of a file, except for the last two lines.  I know that tail -2 filename will give me the last two lines of a file, and head -2 filename will give me the first two lines of a file.  Now, tail +2 filename will give me contents of a file, starting with line 2.  It's not a symmetrical option, but i can at least work with that.  However, the head command lacks this elegant mode; otherwise, i would infer that head +3 filename would give me what i want.  Is there a simple command that can do what i want?  I figure i can implement this in some fugly perl script, but that is not the damn point.

assuming you're using bash.

[identity profile] rwx.livejournal.com 2008-12-05 08:08 am (UTC)(link)
head -$((`wc -l foo | cut -c-8` -2)) foo

Re: assuming you're using bash.

[identity profile] rwx.livejournal.com 2008-12-05 08:10 am (UTC)(link)
also supposing you were speaking metaphorically when you said 'simple.'

Re: assuming you're using bash.

[identity profile] dagbrown.livejournal.com 2008-12-05 08:14 am (UTC)(link)

But that makes two passes through the file and forks an unnecessary cut!

Here, still in two passes, but golfed a bit:

head -$((`wc -l < foo`-2)) foo

And what the hell, here's the fugly perl anyway. One pass, but Perl:


  my @queue;
  while(<>) {
    unshift @queue, $_;
    print pop @queue if scalar(@queue) > 2;
  }

Re: assuming you're using bash.

[identity profile] drieuxster.livejournal.com 2008-12-05 06:37 pm (UTC)(link)
There are no Fuggly Perl Code!!!

Only Fuggly Whiners about solution spaces...

[identity profile] mundivagant.livejournal.com 2008-12-05 08:24 am (UTC)(link)
head --lines=-N

(edit) in this case, head --lines=-2

(edit 2) output of head --version:
head (GNU coreutils) 6.10
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie and Jim Meyering.
Edited 2008-12-05 08:26 (UTC)

[identity profile] mundivagant.livejournal.com 2008-12-05 07:18 pm (UTC)(link)
WE ARE ALL WINNERS!!11111

YAAAAAAAAAAY

GNU

[identity profile] he-who-wanders.livejournal.com 2008-12-06 12:48 am (UTC)(link)

Anytime you can run code written by DJM I say uoi are the winner..

[identity profile] dagbrown.livejournal.com 2008-12-05 08:25 am (UTC)(link)

Heh, I just thought of an evil one.

tac foo | tail +2 | tac
ext_8707: Taken in front of Carnegie Hall (bofh)

[identity profile] ronebofh.livejournal.com 2008-12-05 08:27 am (UTC)(link)
Heh, a friend elsewhere suggested that one, too. Only a weenie OS like Linux would have a utility like tac.

[identity profile] dagbrown.livejournal.com 2008-12-05 08:33 am (UTC)(link)
It actually had its genesis in a bunch of other weenies: the wacky land of BSD ("cat came back from Berkeley waving flags").
ext_8707: Taken in front of Carnegie Hall (imminent destruction)

[identity profile] ronebofh.livejournal.com 2008-12-05 07:11 pm (UTC)(link)
Ironic that FreeBSD doesn't have it anymore...

[identity profile] glaucon.livejournal.com 2008-12-05 07:55 pm (UTC)(link)
tac!
fucking awesome!

this is my new favorite unix command.
(replacing "yes")



ext_243: (bee weapon)

[identity profile] xlerb.livejournal.com 2008-12-05 03:33 pm (UTC)(link)
I was going to suggest that, but with tail -r instead of tac (and the intimation that some systems may provide that functionality by a different means).

after a few rounds of optimization

[identity profile] oh6.livejournal.com 2008-12-05 10:14 am (UTC)(link)
sed -n -e H -e '/\n/h' -e g -e '/\n.*\n/!b' -e P -e D
vatine: Generated with some CL code and a hand-designed blackletter font (Default)

[personal profile] vatine 2008-12-05 11:35 am (UTC)(link)
There is always awk...
ext_86356: (2632)

[identity profile] qwrrty.livejournal.com 2008-12-05 02:37 pm (UTC)(link)
You want fugly? I dare you to do it in bc.

[identity profile] arian1.livejournal.com 2008-12-05 03:23 pm (UTC)(link)
I say you give the +2 filename to the ranger and keep the +3 filename for yourself. Sell off the -2 one and head back into the dungeon.

decaf...

[identity profile] drieuxster.livejournal.com 2008-12-05 06:39 pm (UTC)(link)
switch toooo decalf.....

[identity profile] sweh.livejournal.com 2008-12-05 05:37 pm (UTC)(link)
sed -e '$d' | sed -e '$d'

nasty but it works and is simple to understand.

sed -e 'N;$!P;$!D;$d'
actually does want you want (but has a bug if the file is only 1 line long).

(I googled that... took 5 seconds "sed all but last 2 lines". Don't think I could have created it on my own since I'm not too good with sed pattern buffer mangling)

[identity profile] kerri9494.livejournal.com 2008-12-10 01:33 pm (UTC)(link)
The nurdiness of this thread has me all aflutter. I just had to say.

I was gonna say, "Count the lines, and then get count -2." I had no idea the poetry.