sed (stream editor) is a stream editor that filters and converts text. The working principle of sed is to match the input text lines according to the specified pattern, and then execute the corresponding editing command on the matching lines. sed is ideal for processing text data in a pipeline and can be used to automate text processing tasks in scripts.
1. Basic usage
sed [Options] 'Script' File Name
Script: is a collection of one or more sed editing commands, enclosed in single quotes.
File name: is the text file to be processed. If the file name is omitted, sed will read data from standard input.
Common options
-e Script: Specify the sed script directly on the command line.
-f Script file: Read the sed script from the specified file.
-i[SUFFIX]: Modify the file content directly, rather than output to standard output. If SUFFIX is specified, the original file is backed up and the suffix is added thereafter.
-n: Cancel the default output and only output lines processed by the sed script.
Edit command
a\ Text: Insert new text after matching lines.
i\ Text: Insert new text before matching lines.
c\ Text: Replace the matching line with new text.
d: Delete the matching row.
p: Print the matching line.
s/original mode/new mode/[modifier]: Replace the original mode with the new mode. Modifiers can be g (global replacement), p (print replacement result), i (ignoring case), etc.
Mode Space and Maintenance Space
Mode Space: sed When processing text, a line of text will be read into the mode space and then the commands in the script are executed. After processing a line, sed outputs the contents of the pattern space (unless the -n option is used), and then reads the next line.
Keep Space: is another buffer of sed that can be used to pass data between different parts of the script. Keeping space is often used for complex text processing tasks.
Example
Example 1: Replace text
Replace all "foo" in the file with "bar":
sed 's/foo/bar/g'
Example 2: Delete the line
Delete all lines containing "baz" in the file:
sed '/baz/d'
Example 3: Insert text
Insert a line "Inserted line" before all lines containing "qux" in the file:
sed '/qux/i\Inserted line'
Example 4: Print a specific line
Print lines 2 to 4 in the file:
sed -n '2,4p'
Example 5: Swap two lines using keep space
Suppose you have a file with the following content:
Line 1
Line 2
Line 3
You want to exchange the positions of lines 1 and 3. The following sed scripts can be used:
sed -e '1{h;d}' -e '3{G;s/\(.*\)\n\(.*\)/\2\n\1/}'
explain:
- 1{h;d}: When processing line 1, save it to the hold space (h), and then delete the contents of the mode space (d), without outputting.
- 3{G;s/\(.*\)\n\(.*\)/\2\n\1/}: When processing line 3, append the contents of the retained space to the pattern space (G), and then use the s command to swap two lines.
The output will be:
Line 3
Line 2
Line 1
2. Advanced usage and skills
1.Use address range
sed allows you to specify an address range to execute edit commands for lines within a specific range. The address range can be a line number, a regular expression, or a combination of both.
- Line number range: For example, 2,4 represents lines 2 to 4.
- regular expression range: For example, /start/,/end/ represents the line from the line matching start to the line matching end (including these two lines).
Example:
# Delete lines 2 to 4sed '2,4d' # Replace "foo" with "bar" from the line matching "start" to the line matching "end"sed '/start/,/end/s/foo/bar/g'
2. Multi-command combination
You can use the -e option or semicolon; to combine multiple sed commands.
Example:
# Use the -e option to combine commandssed -e 's/foo/bar/g' -e '2,4d' # Use semicolon combination commandssed 's/foo/bar/g; 2,4d'
3. Advanced applications of mode space and maintain space
Pattern space and hold space are two important buffers for sed. By using them cleverly, you can perform complex text processing tasks.
- Mode Space: Used to store the currently processed rows.
- Keep Space: Used to pass data between different parts of the script.
Example:
# Reverse the line order in the filesed '1!G;h;$!d'
explain:
- 1!G: For non-first rows, append the content of the retained space to the pattern space.
- h: Copy the contents of the mode space to the hold space.
- $!d: For non-last line, delete the contents of the pattern space (no output).
4. Branches and loops
sed provides the functions of branching (b and t commands) and loops (: and ; combinations) that can be used to build complex text processing logic.
- Branch: The b command is used for unconditional redirection, and the t command is used for redirection after successful replacement.
- Loop: Use : to mark a tag, and then use the ; and b commands to implement the loop.
Example:
# Print odd lines in the filesed -n 'n;p'
explain:
- n: Read the next line to the pattern space and overwrite the current line.
- p: The contents of the print mode space.
Since the n command overwrites the current line, this script skips even lines and prints odd lines.
5. Use sed for text analysis and conversion
sed is ideal for text analysis and conversion tasks. By combining regular expressions, address ranges and edit commands, you can easily extract, modify and generate text data.
Example:
# Extract all email addresses in the filesed -n 's/.*<\([^>]*\)>.*/\1/p'
explain:
- s/.*<\([^>]*\)>.*/\1/: Use regular expressions to match the email address and extract it.
- p: Print the matching result.
This example assumes that the email address is surrounded by angle brackets, such as <email@>.
This is the end of this article about the usage of sed commands for Shell programming in Linux. For more related contents of Linux Shell sed commands, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!