Effective way to add two lines at the beginning of a very large file

I have a group of very large (a couple of GB each) text files. I need to add two lines at the beginning of each of these files.

I tried using sed with the following command

sed -i '1iFirstLine' sed -i '2iSecondLine' 

The problem with sed is that it processes the entire file, even if it needed to add only two lines at the beginning, and therefore it takes a lot of time.

Is there an alternative way to do this more efficiently without reading the entire file?

+4
source share
3 answers

You must try

 echo "1iFirstLine" > newfile.txt echo "2iSecondLine" >> newfile.txt cat oldfile.txt >> newfile.txt mv newfile.txt oldfile.txt 
+5
source

It works great and very fast.

 perl -pi -e '$.=0 if eof;print "first line\nsecond line\n" if ($.==1)' *.txt 
+3
source

Adding to the beginning is impossible without overwriting the file (as opposed to adding to the end). You simply cannot “shift” the contents of a file, since no file system supports this. So you should do:

 echo -e "line 1\nLine2" > tmp.txt cat tmp2.txt oldbigfile.txt > newbigfile.txt rm oldbigfile.txt mv newbigfile.txt oldbigfile.txt 

Please note that you need enough disk space to store both files.

+1
source

Source: https://habr.com/ru/post/1433569/


All Articles