Add a line into a huge file

Insert_content_into_file_beginningHandling huge files can be a massive challenge. In a previous post I have shown how to remove a range of lines from a huge file (which can also be used to just read a number of lines out of the file as well). But what if you need to add a line at the beginning of the file or at a specific point?

As I did in my post ‘Remove a range of lines from a huge file‘, I will use a database dump file to show how you can add a specific line to a huge file.

Assuming that we have a rather large sql dump to import, but don’t want to have the changes replicated to the replication slave, we need to add a line at the beginning to disable logging to the binlog. Handling files of a couple of GB can therefore be tricky.

With sed it is possible to add a line at the beginning (or any other line number) of the file as well as at the end of it. The following command would add the above described commands to the sql dump.

sed -e '1iSET SQL_LOG_BIN=0;' -e '$aSET SQL_LOG_BIN=1;' database-dump-file.sql >database-dump-file_no-binlog.sql

The above “sed” command shows 2 editing commands with the -e option. The first editing command starts with “1i” representing the 1. line with the “i” for insert after it. This editing command is followed by the content to be added at that line.

The second edit command starts with “$a” where the “$” sign represents the last line and “a” indicates append. This is again followed by the content to be added.

Check the result file

To check the correct insertion of the lines, use the “head” and “tail” commands. to show the first and last lines of the file.

$ head -n 2 database-dump-file_no-binlog.sql
-- MySQL dump 10.13  Distrib 5.1.57, for debian-linux-gnu (x86_64)
$ tail -n 2 database-dump-file_no-binlog.sql
-- Dump completed on 2014-04-15 17:13:31

The “-n” option on both commands controls the number of lines to show. As the SQL_LOG_BIN only has effect to the current session, it can be omitted if you close the sql session after importing the dump.

Read more of my posts on my blog at

This entry was posted in Linux Administration and tagged . Bookmark the permalink.