Vim | delete the first few lines from a 700 MB file

How to quickly copy the first few lines from a large file without opening the entire file in main memory?


UPDATE

I do not want to trace the source lines x to another file, and then cut the first few lines, I want to update the source file.

+7
source share
7 answers

Not really vim, but cut out the first 10 lines you could use

 tail --lines=+10 somefile.txt > newfile.txt 
+6
source
 tail -n+11 somefile.txt | vim - 

Cut the first 10 * lines and open the file for editing without creating a temporary file. Note that the file will not have a name in vim when you open it that way. This is the only drawback.

* Note that although I used 11 in a command, this starts at line 11. Thus, it will chop off the first 10 lines.

+3
source

The original question actually never answered. I believe this solution is:

 sed -i 's/`head -n 500 foo.txt`//' foo.txt 

This would exclude the first 500 lines of the file without having to create a temporary file. (Actually, you might need to do head -n 499). I think this is really useful, as a single-line scanner, for cleaning log files without just erasing the entire log.

+3
source
 $ seq 1 502 > foo.txt $ sed -i 1,500d foo.txt $ cat foo.txt 501 502 
+1
source

vim will always want / need to read in the whole file, so there is no way to do this with (only) vim. Darcara's proposal looks good.

This process will always include copying everything except the first part of the file to another, so I see no way to quickly do this.

0
source

Depending on what you can do with the file, you may be better off using sed or awk to edit such a large file.

0
source

What about..

  • split the source file into 2 parts. ( p1 : lines 0 - x ) ( p2 : lines x+1 - n )
  • edit p1 since you want to edit the first lines of x . We will call him p1'
  • combine p1' and p2

In short

  • filep1 and p2
  • p1p1'
  • p1' + p2new_file

Teams

  • use split or cut

  • use vim or the editor of your choice.

  • use cat to combine

0
source

All Articles