cancel
Showing results for 
Search instead for 
Did you mean: 

Delete last two lines

SOLVED
Go to solution
parmy_4
Occasional Contributor

Delete last two lines

Hi,
I have a file in which I don't need the last two lines. How can run a command to achieve this?

I have a work around for my collegue but he is not happy with it.

sed '$d' my_file | sed '$d'

I am expecting some thing like

sed -e '$d' -e '$d' my_file.

Please help us.
Parmy
17 REPLIES
Pete Randall
Outstanding Contributor

Re: Delete last two lines

Parmy,

Use "sed -e '$d' my_file |sed -e '$d'"

that should do it.


Pete

Pete
harry d brown jr
Honored Contributor

Re: Delete last two lines

vi my_file<
live free or die
harry
Live Free or Die
Jean-Luc Oudart
Honored Contributor

Re: Delete last two lines

with vi cmd
vi +$ my_file<
Regards
Jean-Luc
fiat lux
harry d brown jr
Honored Contributor

Re: Delete last two lines

Another way:

expr `wc -l my_file | cut -d" " -f1` - 2 | xargs echo | sed -e "s/^/head -/" -e "s/$/ my_file/" | sh > new_my_file

live free or die
harry
Live Free or Die
john korterman
Honored Contributor
Solution

Re: Delete last two lines

Hi,

I think this works (at least it seems so on my screen):

# tail -2 | comm -23 -

regards,
John K.
it would be nice if you always got a second chance
Jean-Luc Oudart
Honored Contributor

Re: Delete last two lines

awk solution :
awk -v nb=$(wc -l my_file | cut -f1 -d" ") '{if(NR>(nb - 2)) exit; print}' my_file

Regards,
Jean-Luc
fiat lux
Elmar P. Kolkman
Honored Contributor

Re: Delete last two lines

A possible solution:

head $(wc -l my_file | awk '{print "-" $1 - 2}') my_file

In vi you can use the command:
$-1,$d
to delete the last two lines. Should work in ed too.

Or a complete awk solution:
awk '{ if (NR > 2) { print prev2 } prev2=prev1;prev1=$0}' < my_file
Every problem has at least one solution. Only some solutions are harder to find.
Bharat Katkar
Honored Contributor

Re: Delete last two lines

Hi
This works ..but don't know you like it or not.

# let A=`cat file1 | wc -l`-2
# head -$A file1 >file2

That works.
Regards,
You need to know a lot to actually know how little you know
Jan Sladky
Trusted Contributor

Re: Delete last two lines

hi Parmy,

I have no HP-UX where to try it but on Linux
exists the tac command (opposit to cat)

so maybe the following row will help you

tac file | sed '1,2d' | tac | tee file1


br Jan
GSM, Intelligent Networks, UNIX
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.