sed Command in Linux
sed is a powerful text editing tool. Short for Stream Editor, sed allows us to find, replace, delete, insert, and transform text in a file or stream - all without opening a text editor. It is particularly useful for automating repetitive text-based tasks, manipulating files without opening them in a text editor, and performing search-and-replace operations.
What is sed Command ?
sed is a non-interactive stream editor. It reads text line-by-line from a file or input stream, applies a set of instructions, and writes the modified output to standard output (by default).
Basic Syntax of sed Command:
sed [options] 'command' file
- options : Modify sed's behavior, such as -i for in-place editing or -n to suppress automatic output.
- command : Contains editing instructions, like the substitute command s or the delete command d.
- file : The file(s) sed will process; can be piped from another command.
Example of sed Command in Linux
1. Simple Find and Replace
sed 's/error/success/' file.txt
🔹 Replaces the first occurrence of "error" with "success" in each line.
2. Global Replacement
sed 's/error/success/g' file.txt
🔹 Replaces all occurrences of "error" in each line.
To replace a specific occurrence, like the second instance of a pattern, specify it after the final delimiter. For example : sed 's/unix/linux/2' configfile.txt
Also, you can combine n with g to replace from the nth occurrence to all occurrences in a line.
3. In-Place File Editing
sed -i 's/http/https/g' index.html
🔹 Modifies the file in place (no need to redirect output).
📝 Tip: Add a backup extension if you want to preserve the original :
sed -i.bak 's/http/https/g' index.html
4. Delete Specific Lines
sed '2d' file.txt
🔹 Deletes the 2nd line.
sed '3,5d' file.txt
🔹 Deletes lines 3 to 5.
sed '/^$/d' filename.txt
🔹 Deletes empty lines
5. Print Specific Lines
sed -n '2p' file.txt
🔹 Prints only the 2nd line.
sed -n '3,6p' file.txt
🔹 Prints lines 3 to 6 only.
6. Insert a Line Before or After a Pattern
- Insert before a match:
sed '/pattern/i New line before pattern' file.txt
- Insert after a match:
sed '/pattern/a New line after pattern' file.txt
7. Replace Only on a Specific Line
sed '5s/foo/bar/' file.txt
🔹 Only replaces "foo" with "bar" on line 5.
Regular expressions with sed
sed supports regular expressions (regex) for powerful pattern matching and manipulation.
- ^ : Matches the beginning of a line.
- $ : Matches the end of a line.
- . : Matches any single character.
- * : Matches zero or more occurrences of the preceding character.
- + : Matches one or more occurrences of the preceding character (requires -E or -r).
- [] : Matches any character within the brackets.
- () : Used for grouping subexpressions and backreferences.
Using sed command in Pipelines
sed works seamlessly in Unix pipelines :
cat access.log | sed -n '/404/p'
🔹 Filters only lines with 404 status codes from a web log.
Or :
ps aux | sed -n '/apache2/p'
🔹 Show only processes related to apache2.
Tips and Tricks
- Combine with find for recursive edits :
find . -name "*.html" -exec sed -i 's/http/https/g' {} +
sed command Use cases
- Log analysis : Filtering log files for errors or specific events.
- Configuration management : Editing configuration files, such as updating paths or changing settings.
- Batch processing : Applying the same operation to multiple files.
- Data extraction and transformation : Extracting relevant data from structured files or transforming data formats.
The sed command is a great tool for working with text in Linux environments.