Operating System - HP-UX
1751894 Members
5237 Online
108783 Solutions
New Discussion юеВ

Re: Delete last two lines

 
SOLVED
Go to solution
Rodney Hills
Honored Contributor

Re: Delete last two lines

Here is a perl one-liner that only requires one pass through the file.

perl -ne 'push(@last2,$_); if (scalar @last2 > 2) { print shift @last2}'

By pushing each line onto a stack this script will not print the last 2 lines.

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: Delete last two lines

Parmy,

Assigning points to indicate which solution you think works the best is what helps the forum help others...

-- Rod Hills
There be dragons...
Rick Garland
Honored Contributor

Re: Delete last two lines

cat $file | sed '1,2d'
Hein van den Heuvel
Honored Contributor

Re: Delete last two lines


Here is a one-pass in awk which does no create a temp file.
It uses a 'circular buffer' of the target 'missing line' count 'L'.
This is done with an array 'line' indexed by the current record number (NR) modulus that L.
You provide a desired value for L as argument.
We start printing from the array only after L lines have come by, 'falling behind' by L lines.

Sounds tricky? Here is the 'one line' code:

awk -vL=2 '{if(NR>L){print line[(NR-L)%L];line[NR%L]=$0}}' your-file


Hein.
Elmar P. Kolkman
Honored Contributor

Re: Delete last two lines

Hein, your solution is a variant of my awk solution... but my mathematic mind says you need to remove the -L in your solution. It does nothing in the end... since (NR-L)%L equals NR%L.
And you have a closing curly bracket wrong, resulting in missing the first lines...

So the right solution would be:

awk -vL=2 '{if(NR>L){print line[NR%L];}line[NR%L]=$0}' my_file
Every problem has at least one solution. Only some solutions are harder to find.
Elmar P. Kolkman
Honored Contributor

Re: Delete last two lines

Strange point assignment... I've tested the highest rated solution, but it doesn't seem to work. At least nog on the /etc/passwd files I have on Linux and HP-UX... But it could be me...
Every problem has at least one solution. Only some solutions are harder to find.
Bharat Katkar
Honored Contributor

Re: Delete last two lines

It looks like Parmy is taking it very lightly and on the contrary all the responders are struggling to be as perfect as possible..
You need to know a lot to actually know how little you know
Hein van den Heuvel
Honored Contributor

Re: Delete last two lines

Elmar> Hein, your solution is a variant of my awk solution...

Ayup, just a touch more general purpose. I had missed the awk part of your solution.

Elmar> but my mathematic mind says you need to remove the -L in your solution.

Ayup, a simplication I overlooked this time.

Elmar> It does nothing in the end... since (NR-L)%L equals NR%L.

Ayup, other then to make the code more clear.

Elmar> And you have a closing curly bracket wrong, resulting in missing the first lines...

Ooops! Thanks for catching that.

Groetjes,
Hein.