Operating System - HP-UX
1753394 Members
7239 Online
108792 Solutions
New Discussion юеВ

Re: Trimming the file size

 
SOLVED
Go to solution
gany59
Regular Advisor

Trimming the file size

How to delete the last 1000 lines from the file..
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: Trimming the file size

Hi :

Given that you know the first line number in the file after which you don't want anything kept, do by example:

# sed -e 200,$d' file

In this example, I wanted to start deleting at the 200th line until the end of the file.

Remember that 'wc -l file' offers you the count of the number of total lines in a file.

You may also want to revisit your similar, earlier question on this topic:

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1367025

Regards!

...JRF../
gany59
Regular Advisor

Re: Trimming the file size

Hi James

Thx for ur quick and valuable reply...
Raj D.
Honored Contributor

Re: Trimming the file size

gany59,

- Open the file in vi .
- go to the position at before last 1000 lines. ( You can see current line position by Ctrl +g ) [ To go to a particular line Esc :Linenumber ]
- Esc 1000dd #It will delete 1000 lines from there.
- Save and exit vi.


Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: Trimming the file size

GAny59,

Also chekc this out:

Even you dont know the first line number in the file after which you want to delete:


# To delte last 1000 lines of the file:
1. # cat filename | sed -e :a -e '$d;N;2,1000ba' -e 'P;D' > trimmed_file.out

2. # cat filename | cat -n | sort -rn | cut -c8- | sed -e '1,1000d' | cat -n | sort -rn | cut -c8- > trimmed_file.out


Cheers,
Raj.


" If u think u can , If u think u cannot , - You are always Right . "
Suraj K Sankari
Honored Contributor

Re: Trimming the file size

Hi,
The best option is sed

In this example, sed will delete lines 1-10 of the output:

$ sed -e '1,1000d' /tmp/myfile

When we separate two addresses by a comma, sed will apply the following command to the range that starts with the first address, and ends with the second address. In this example, the 'd' command was applied to lines 1-10, inclusive. All other lines were ignored.

you can give 20,30 or any range.

Suraj
Dennis Handly
Acclaimed Contributor

Re: Trimming the file size

I couldn't get sed to take $-999,$. I suppose I could have used wc to calculate that value.
Here is an ex(1) solution:
ex <<\EOF file
$-999,$d
wq
EOF