Function Overview:sed
is a stream editor that reads text from files or input streams line by line, edits the text according to user-specified patterns or commands, and then outputs the result to the screen or a file. When used in conjunction with regular expressions, it is incredibly powerful.
Syntax:
sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)
Explanation:sed
first stores each line of the text in a temporary buffer called the “pattern space.” It then processes the content of this buffer according to the given sed
commands. Once the processing is complete, the result is output to the terminal, and sed
moves on to the next line. The content of the file itself is not altered unless the -i
option is used. sed
is mainly used to edit one or more text files, simplify repeated text file operations, or create text transformation scripts. Its functionality is similar to awk
, but sed
is simpler and less capable of handling column-specific operations, while awk
is more powerful in that regard.
Options:
-e
: Use the specified commands to process the input text file.-n
: Suppress automatic output (only prints lines modified when used with thep
command).-h
: Display help information.-V
: Display version information.
Parameters:
command
: The command to be executed.file(s)
: One or more text files to be processed.scriptfile
: A file containing a list of commands to execute.
Common Actions:
a
: Append text after the current line.i
: Insert text before the current line.c
: Replace the selected lines with new text.d
: Delete the selected lines.D
: Delete the first line of the pattern block.s
: Replace specified characters.h
: Copy the pattern block’s content to an internal buffer.H
: Append the pattern block’s content to the internal buffer.g
: Retrieve content from the internal buffer and replace the text in the current pattern block.G
: Retrieve content from the internal buffer and append it to the current pattern block.l
: List non-printable characters in the text.L
: Similar tol
, but specifically for handling non-ASCII characters.n
: Read the next input line and apply the next command to it instead of reapplying the first command.N
: Append the next input line to the current pattern block and insert a new line between them, changing the current line number.p
: Print the matching lines.P
: Print the first line of the pattern block.q
: Quitsed
.b label
: Branch to the location marked bylabel
in the script; if the label doesn’t exist, the branch goes to the end of the script.r file
: Read lines from a file.t label
: Conditional branch to a marked location, starting from the last line. If the condition is met, or aT
/t
command is used, the branch jumps to the specified label or the end of the script.T label
: Error branch. If an error occurs, this branches to the labeled command or the end of the script.w file
: Write the processed block of the pattern space to the end of a file.W file
: Write the first line of the pattern space to the end of a file.!
: Execute the following commands on all lines not selected by the current pattern.=
: Print the current line number.#
: Extend comments to the next newline character.
Replacement Commands:
g
: Global replacement within a line (used with thes
command).p
: Print the line.w
: Write the line to a file.x
: Exchange the text in the pattern block with the text in the internal buffer.y
: Translate one character to another (not used with regular expressions).&
: Reference to the matched string.
Basic Regular Expression (BRE) Syntax in sed
:
^
: Match the beginning of a line.$
: Match the end of a line..
: Match any single character except a newline.*
: Match zero or more of the preceding characters.[]
: Match a single character from a specified range.[^]
: Match a single character not in the specified range.(..)
: Capture a substring.&
: Save the matched text for later use in replacements.<
: Match the start of a word.>
: Match the end of a word.x{m}
: Match exactlym
occurrences ofx
.x{m,}
: Match at leastm
occurrences ofx
.x{m,n}
: Match betweenm
andn
occurrences ofx
.
To match the start of a word, use \<
. To match the end of a word, use \>
.
Extended Regular Expression (ERE) Syntax in sed
:
\b
: Match a word boundary (not supported by default insed
regular expressions).+
: Match one or more occurrences of the preceding character.
Practical Examples:
1 Print specific lines:
To print only lines 1 and the last line:
sed -n '1p;$p' test.txt
2 Delete lines:
To delete the second line:
sed '2d' filename
3 Basic match and replace:
Replace spaces with hyphens:
echo "hello world" | sed 's/ /-/g'
4 Advanced match and replace:
Reverse words in a string:
echo "abc def ghi" | sed 's/\([a-zA-Z]*\) \([a-zA-Z]*\) \([a-zA-Z]*\)/\3 \2 \1/'
5 Multiple edits:
Replace “Hello” with “Hi” and “Goodbye” with “Farewell” in one command:
sed 's/Hello/Hi/; s/Goodbye/Farewell/' example.txt
6 Read a file:
Insert content from an external file after lines matching a pattern:
sed '/Line 2/r extra.txt' data.txt
7 Write to a file:
Save processed content into a new file:
sed 's/World/Everyone/' input.txt > output.txt
In summary, sed is a versatile and efficient tool for editing text in a stream, offering powerful pattern matching and text transformation capabilities when combined with regular expressions. From basic line printing to advanced text manipulation, sed serves a wide range of text processing needs.