Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I append a line to the beginning of a very large file in Linux?

Tags:

linux

unix

I have a large text file of ~45 GB, and need to append a line both at the beginning and at the end of the file. What is the quickest way to accomplish this? I don't have access to any big data framework such as Hadoop, etc.

In addition, if I have to also do a string replace in this large file, is there a similar efficient way?

I tried with echo command and it really is taking ages.

like image 747
London guy Avatar asked Feb 14 '23 09:02

London guy


2 Answers

You can use this sed to add a line in the beginning:

sed -i '1s/^/your heading line\n/' file

And to the end:

echo "my new line" >> file

Test

$ cat a
hello
bye

$ sed -i '1s/^/new line sample\n/' a

$ cat a
new line sample
hello
bye


$ echo "my new line" >> a

$ cat a
new line sample
hello
bye
my new line
like image 194
fedorqui 'SO stop harming' Avatar answered Mar 09 '23 00:03

fedorqui 'SO stop harming'


Using sed, you could say:

sed -e '1ifoo' -e '$abar'

and this would insert foo at the top of the file and bar at the end of the file.

$ seq 5 | sed -e '1ifoo' -e '$abar'
foo
1
2
3
4
5
bar
like image 45
devnull Avatar answered Mar 09 '23 01:03

devnull