1 Answers
Practical `sed` One-Liners for System Administration
1. Replacing Text
One of the most common uses of `sed` is to find and replace text. The `s` command (substitute) is your go-to for this.
- Global Replacement: Replace all occurrences of 'old_text' with 'new_text' on each line.
sed 's/old_text/new_text/g' filename.txt - Case-Insensitive Global Replacement: Add the `i` flag for case-insensitive matching.
sed 's/old_text/new_text/gi' filename.txt - Replacing only the Nth occurrence: Replace only the first, second, or Nth occurrence on each line.
sed 's/old_text/new_text/2' filename.txt(replaces the second occurrence)
2. Deleting Lines
You can delete lines based on patterns or line numbers.
- Delete lines containing a pattern:
sed '/pattern_to_delete/d' filename.txt - Delete blank lines:
sed '/^$/d' filename.txt - Delete lines N to M:
sed '10,20d' filename.txt(deletes lines from 10 to 20) - Delete lines from N to end of file:
sed '10,$d' filename.txt
3. Inserting and Appending Lines
The `i` (insert) and `a` (append) commands allow you to add new lines relative to a pattern or line number.
- Insert a line before a pattern:
sed '/pattern/i\This line will be inserted above the pattern.' filename.txt - Append a line after a pattern:
sed '/pattern/a\This line will be appended below the pattern.' filename.txt - Insert at the beginning of the file:
sed '1i\HEADER LINE' filename.txt - Append at the end of the file:
sed '$a\FOOTER LINE' filename.txt
4. Extracting Specific Data
Use `sed` to filter and display only the lines you need.
- Print lines containing a pattern: The `-n` option suppresses default output, and `p` prints matched lines.
sed -n '/error/p' /var/log/syslog - Print lines between two patterns (inclusive):
sed -n '/StartPattern/,/EndPattern/p' filename.txt - Extract specific fields (e.g., from `/etc/passwd`):
sed -n 's/^\([^:]*\):[^:]*:[^:]*:\([^:]*\):.*$/\1 - \2/p' /etc/passwd
This example extracts username and GID.
5. In-Place Editing
The `-i` option allows `sed` to modify files directly, which is extremely useful for configuration management. Always back up your files before using `-i`!
- Replace in file (creates backup):
sed -i.bak 's/old_value/new_value/g' /etc/config.conf - Replace in file (no backup):
sed -i 's/old_value/new_value/g' /etc/config.conf
6. Processing Specific Line Numbers
You can apply `sed` commands to specific lines or ranges.
- Replace on a specific line:
sed '5s/foo/bar/' filename.txt(replaces 'foo' with 'bar' on line 5) - Delete a specific line:
sed '5d' filename.txt(deletes line 5)
7. Using Multiple Commands
Combine multiple `sed` commands using `-e` or semicolons.
- Multiple commands with `-e`:
sed -e 's/foo/bar/g' -e '/baz/d' filename.txt - Multiple commands with semicolon:
sed 's/foo/bar/g; /baz/d' filename.txt
Important Note: When performing in-place edits on critical system files, always create a backup first. The `-i.bak` option is highly recommended to prevent accidental data loss.
These `sed` one-liners cover a broad spectrum of everyday system administration tasks. By understanding these fundamental operations, you can efficiently manipulate text files, automate routine maintenance, and streamline your workflow on Linux systems. Practice is key to mastering `sed`'s full potential!
Know the answer? Login to help.
Login to Answer