Mastering Text Insertion in Linux Files Using sed Command
The i
command in sed is used to insert text before a line. The general syntax for the i
command is:
sed '/pattern/i\text-to-be-inserted' filename
In this tutorial, we will focus on inserting text into files using sed command.
- 1 Sed Insertion Saved us Fom Months of Manual Editing
- 2 Insert Text at a Specific Line
- 3 Insert Text at the Beginning of a File
- 4 Insert Before Last Line
- 5 Insert Text before a Specific Match
- 6 Insert Text between Two Specific Lines
- 7 Conditional Insertions Based on Patterns
- 8 Insert Escape Characters
- 9 Insert Variable Value and Command Substitution
- 10 Insert Multiple Lines around a Specific Pattern
- 11 Handling Multiple Files
Sed Insertion Saved us Fom Months of Manual Editing
One day, a small local ISP approached me with an intriguing problem. They had recently migrated their user database, and in the process, a crucial format change was overlooked.
They had a list of more than 100,000 user records, and each record was missing an important identifier that was needed for their new system. Each record was in the format:
username:hashed-password:account-status
But the new system needed:
username:hashed-password:UID:account-status
Here, UID
stands for User Identifier, a unique alphanumeric string for each user.
Without the UID, the ISP’s entire billing and usage tracking system would malfunction, possibly leading to significant revenue loss and unhappy customers.
Manually inserting these UIDs for over 100,000 records was not only impractical but would take months with the small team they had.
I decided to use the sed
command to automatically insert UIDs. I wrote a simple script that would generate a UID for each user and then use sed
to insert it into the appropriate place in each record.
Here’s a simplified example of the command I used:
sed -i 's/\(username:hashed-password:\)\(account-status\)/\1UID:\2/' user-database.txt
With that single command, sed
processed the file in less than 10 minutes.
Compared to manually editing the file, which could have taken a small team over 800 hours (considering 5 minutes to edit and verify each record).
Insert Text at a Specific Line
To insert text at a specific line number in a file using sed, you can use the following syntax:
sed 'LINE_NUMBER i\text-to-be-inserted' filename
For example, if you want to insert the text “This is line 3.” at the third line of the ‘example.txt’ file, you can use the following command:
sed '3 i\This is line 3.' example.txt
Output:
Apple Banana This is line 3. Cherry
In this example, ‘3’ is the line number where the text “This is line 3.” is inserted before the line.
Please note that this command does not modify the original file. It only displays the modified content in the terminal.
If you want to save the changes to the file, you can redirect the output to a new file or use the -i
option with sed.
For example, to save the changes to a new file:
sed '3 i\This is line 3.' example.txt > newfile.txt
Or, to modify the original file in place, you can use the -i
option:
sed -i '3 i\This is line 3.' example.txt
Insert Text at the Beginning of a File
To insert text at the beginning of a file, you can use the line number ‘1’ in the sed command as follows:
sed '1 i\text-to-be-inserted' filename
For example, if you want to insert the text “This is the start of the file.” at the beginning of the ‘example.txt’ file, you would use the following command:
sed '1 i\This is the start of the file.' example.txt
Output:
This is the start of the file. Apple Banana Cherry
In this example, ‘1’ is the line number which indicates the start of the file. Therefore, the text “This is the start of the file.” is inserted at the beginning of the file.
Insert Before Last Line
To insert text before the last line of a file, you can use the following command:
sed '$!b; i\This is the second last line.' example.txt
Output:
Apple Banana This is the second last line. Cherry
In this command, $!b
skips all lines except the last one, and the ‘i’ command inserts the text “This is the second last line.” before the last line.
Insert Text before a Specific Match
To insert text before a line containing a specific pattern, you can use the i
command in sed like this:
sed '/pattern/i\text-to-be-inserted' filename
For example, if you want to insert the text “This is a fruit.” before the line containing “Cherry” in the ‘example.txt’ file, you can use the following command:
sed '/Cherry/i\This is a fruit.' example.txt
Output:
Apple Banana This is a fruit. Cherry
In this command, ‘/Cherry/’ is the pattern that sed searches for in the file. Once it finds a line with the pattern, it inserts the text “This is a fruit.” before that line.
Insert Text between Two Specific Lines
You can combine the i
and a
commands in sed to insert text between two specific lines.
For example, if you want to insert text between the second and third lines of the example.txt
file, you can use the following command:
sed '2 a\This is between second and third line' example.txt
Output:
Apple Banana This is between second and third line Cherry
In this example, ‘2’ is the line number after which the text “This is between second and third line” is inserted.
Conditional Insertions Based on Patterns
You can insert text before or after a line only if that line matches a specific pattern.
For example, if you want to insert text before a line that contains “Banana” in the ‘example.txt’ file, but only if that line also contains “Apple”, you can use the following command:
sed '/Banana.*Apple/ i\This is a fruit.' example.txt
Let’s assume that the ‘example.txt’ file to contain a line with both “Apple” and “Banana”:
Apple Banana Apple Cherry
Output:
Apple This is a fruit. Banana Apple Cherry
In this example, the pattern ‘/Apple.*Banana/’ matches the line “Banana Apple”, so the text “This is a fruit.” is inserted before that line.
Insert Escape Characters
Escape characters are characters that are used to represent certain special characters like newline, tab, etc. In sed, escape characters are preceded by a backslash ().
For example, if you want to insert a line containing a tab, a newline, and a backslash at the beginning of the ‘example.txt’ file, you can use the following command:
sed '1 i\\\t\\n\\' example.txt
Output:
\t\n\ Apple Banana Cherry
In this command, ‘1’ is the line number where the text “\t\n” is inserted.
Since the backslash is used to escape characters in sed, you need to use two backslashes () to represent a single backslash.
Insert Variable Value and Command Substitution
You can also insert the value of variables or the output of commands using sed.
For example, if you want to insert the current date and time at the beginning of the ‘example.txt’ file, you can use the following command:
sed "1 i\\$(date)" example.txt
Output:
Sun Sep 5 08:21:10 UTC 2023 Apple Banana Cherry
In this command, 1
is the line number where the output of the date
command is inserted.
Insert Multiple Lines around a Specific Pattern
If you want to insert two lines “This is the first line.” and “This is the second line.” before and after each line containing “Banana” in the ‘example.txt’ file, you can use the following command:
sed -e '/Banana/ i\This is the first line.' -e '/Banana/ a\This is the second line.' example.txt
Output:
Apple This is the first line. Banana This is the second line. Cherry
In this command, ‘/Banana/’ is the pattern that matches lines containing “Banana”.
In the above command, the first -e
option inserts “This is the first line.” before each line containing “Banana”, and the second -e
option appends “This is the second line.” after each line containing “Banana”.
Handling Multiple Files
sed
can also handle multiple files at once, which allows you to insert text into multiple files or insert different text into multiple files with a single command.
For example, if you want to insert the line “This is a fruit.” at the beginning of two files, ‘example1.txt’ and ‘example2.txt’, you can use the following command:
sed -i '1 i\This is a fruit.' example1.txt example2.txt
In this command, ‘1’ is the line number where the text “This is a fruit.” is inserted in both files. The -i
option modifies the files in place.
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.