Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Insert a Line at Specific Line Number
Inserting a line at a specific line number in a file is a common task in Linux system administration and text processing. The sed (stream editor) command provides a powerful and flexible way to accomplish this task efficiently from the command line.
What is sed?
sed stands for "stream editor" and is a command-line utility that allows you to modify file contents by applying various operations such as replacing text, deleting lines, and inserting lines. It processes text line by line, making it ideal for automated text processing and bulk file modifications.
Basic Syntax for Line Insertion
To insert a line at a specific line number using sed, use the i command followed by the text you want to insert. The i command tells sed to insert the specified line before the target line number.
sed 'line_number i text_to_insert' filename
Examples
Insert Line at Specific Position
To insert "This is a new line" at line 3 in file.txt
sed '3i This is a new line' file.txt
Insert Line at Beginning of File
To insert a line at the very beginning (line 1)
sed '1i Header line added' file.txt
Insert Line at End of File
To append a line at the end of the file, use the a command with $ (last line)
sed '$a Footer line added' file.txt
Modifying the Original File
By default, sed outputs the modified content to standard output without changing the original file. To save changes directly to the file, use the -i option
sed -i '3i This line is saved permanently' file.txt
For safety, you can create a backup before modifying
sed -i.bak '3i This line is saved permanently' file.txt
Advanced Usage
| Command | Description | Example |
|---|---|---|
i |
Insert before line | sed '5i New line' file.txt |
a |
Append after line | sed '5a New line' file.txt |
-n |
Suppress default output | sed -n '3i New line' file.txt |
-i |
Edit file in-place | sed -i '3i New line' file.txt |
Insert Multiple Lines
To insert multiple lines, use for line breaks
sed '2i Line 1\nLine 2\nLine 3' file.txt
Insert with Pattern Matching
Insert before lines matching a pattern
sed '/pattern/i Text to insert before pattern' file.txt
Practical Example
Consider a configuration file config.txt with the following content
server=localhost port=8080 debug=false
To insert a database configuration line after line 2
sed '2a database=myapp_db' config.txt
Output
server=localhost port=8080 database=myapp_db debug=false
Limitations and Alternatives
While sed is excellent for simple line insertions, it has limitations when dealing with complex multi-line insertions or when you need more sophisticated text processing. For such scenarios, consider using awk, perl, or text editors like vim with command mode.
Conclusion
The sed command provides a simple and efficient way to insert lines at specific positions in files. Its i and a commands, combined with options like -i for in-place editing, make it an essential tool for Linux text processing and automation tasks.
