Operating System - HP-UX
1833465 Members
2876 Online
110052 Solutions
New Discussion

Delete first lines from text file

 
Mauro_8
Frequent Advisor

Delete first lines from text file

Hi,

I want to delete the first lines from text file and keep only the last 50 lines of the file. How can I do it ? Using sed ?

Cheers,
Mauro
6 REPLIES 6
Justo Exposito
Esteemed Contributor

Re: Delete first lines from text file

Hi Mauro,

You can use tail -50 fichname > fichtmp

Regards,

Justo.
Help is a Beatiful word
Uday_S_Ankolekar
Honored Contributor

Re: Delete first lines from text file

Easy way would be..

tail -50 filename > newname

-USA..
Good Luck..
Justo Exposito
Esteemed Contributor

Re: Delete first lines from text file

Hi Mauro,

With sed is like:
num=`wc -l fich| cut -d" " -f1`
(( num1=num - 50 ))
sed -e ''$num1','$num'd' fich

Regards,

Justo.
Help is a Beatiful word
Justo Exposito
Esteemed Contributor

Re: Delete first lines from text file

Hi Again,

You must observe that in this line:
sed -e ''$num1','$num'd' fich
before $ character are two ' characters and not one ".

Regards,

Justo.
Help is a Beatiful word
Mauro_8
Frequent Advisor

Re: Delete first lines from text file

Hi,

I just want to open the file, delete the lines and close it without distroying the file because this file is a log file and a process writes in this file continuosly if I have to rename to a temp file and then back it to orginal name this process will lose the point.

Thanks once more,
Mauro
Nelson Mina
New Member

Re: Delete first lines from text file


tail -50 logfile > newfile
cat newfile > logfile

This won't close the pointer to the file. You have a slight risk of loosing data if it was written to the log file after the "tail" but before the "cat" command.