Vladimir Evdokimov

My portfolio and blog, mostly about Ruby on Rails

Two useful console snippets for huge files

Once I needed to prepend a line (use this_database;) to a tremendous sql dump. None of my text editors could open it whatsoever.

I found a shell one liner which did this job perfectly. But I forgot a semicolon at the end of the line (oops.. haven’t backed up initial sql file). So I needed to remove that line and add proper one.

As a conclusion of this cool story see two commands below.

Prepend a line to huge file

echo "I am a first line!"|cat - huge_file.sql > /tmp/out && mv /tmp/out huge_file.sql

I could change the output file’s name, just to be on the safe side, but didn’t want another 5GB file on my SSD.

Remove the first line from a huge file:

sed 1d < bigfile.txt >bigfile2.txt