Common sed Parameters
The sed
(stream editor) command is a powerful tool in the Linux command line that allows you to perform various text manipulations on input streams. Here are some of the most common sed
parameters:
1. Addressing
- Line numbers:
sed '2,5d'
- Deletes lines 2 through 5. - Regular expressions:
sed '/^#/d'
- Deletes lines starting with a hash symbol. - Ranges:
sed '2,/^$/d'
- Deletes lines from the second line until the first blank line.
2. Editing Commands
- Substitution:
sed 's/old/new/'
- Replaces the first occurrence of "old" with "new". - Global substitution:
sed 's/old/new/g'
- Replaces all occurrences of "old" with "new". - Deletion:
sed '2d'
- Deletes the second line. - Insertion:
sed '2i\new line'
- Inserts "new line" before the second line. - Appending:
sed '2a\new line'
- Appends "new line" after the second line.
The sed
command is highly versatile and can be combined with other Linux tools, such as awk
and grep
, to create powerful text processing workflows. By understanding the common sed
parameters, you can efficiently manipulate text data, automate repetitive tasks, and streamline your Linux command-line operations.