1834609 Members
2865 Online
110069 Solutions
New Discussion

Trim a file

 
SOLVED
Go to solution
Coolmar
Esteemed Contributor

Trim a file

We have a huge file that we need to remove the first 5000 lines from. We can't use "sed" because we can't pipe it to a new file. We need to remove the lines right from the original.

Thanks!
8 REPLIES 8
Ivan Ferreira
Honored Contributor

Re: Trim a file

Create a file called delete_lines.vi like this:

:1,20 d
:wq

Then run vi in this way:

vi - trimfile < delete_lines.vi

Basically, what you are doing is to run vi in "batch" mode executing the commands indicated in delete_lines.vi
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Coolmar
Esteemed Contributor

Re: Trim a file

The problem with vi is that the file is too large for the memory...otherwise he would just do a dd within vi. Is there another way?
Geoff Wild
Honored Contributor

Re: Trim a file

Remove the first 5000?

How many does that leave? 100?

tail - 100 > tempfile
cat tempfile > origfile

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Coolmar
Esteemed Contributor

Re: Trim a file

No, it leaves around 200,000....that is why I need to trim that file as is. I cannot manipulate it and write it to another file.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Trim a file

Hi:

# perl -ni.old -e 'next if 1..5000;print' file

This will perform an "inplace" update of 'file' leaving an unmodified copy as 'file.old'. If you don't want to save the unmodified copy, use :

# perl -ne 'next if 1..5000;print' file

Regards!

...JRF...
Ivan Ferreira
Honored Contributor

Re: Trim a file

One last try, with the ex editor this time:

cat delete_lines.ex
1,5000 d
x

ex - file < delete_lines.ex
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Coolmar
Esteemed Contributor

Re: Trim a file

Thanks for the responses guys....I went with James' as it was exactly what I was looking for.
Ivan Ferreira
Honored Contributor

Re: Trim a file

I really need to learn perl right now! =(
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?