Sed in Linux text processing tool
Concept:
Sed is an online editor that processes a row of content at a time. During processing, the currently processed rows are stored in the temporary cache, which is called "pattern space". Then, you can use the command to process the content in the buffer. After processing, output the content of the cache area. Next, process the next row, and repeat until the end of the file. The file content has not changed, unless you use redirection to store the output. Sed is mainly used to edit one or more files.
Format:
Sed [-nefri] 'command' input text
Common options:
-N: silent mode. In general sed usage, all data from STDIN is listed on the terminal. However, if the-n parameter is added, only the row (or action) that has been specially processed by sed will be listed.
-E: directly edit the sed action in the Command column mode.
-F: Write the sed action directly in a file.-f filename can execute the sed action in filename.
-R: The sed Action supports extending the syntax of the regular expression (by default, the syntax of several regular expressions)
-I: directly modify the file content to be read, rather than output on the screen.
Action Description:
[N1 [, n2] Common commands: these commands do not necessarily exist. Generally, they represent "number of rows selected for an action". For example, if my actions need to be performed between 10 and 20 rows, then "10, 20 [Action Behavior]"
Common commands:
A: append. a can be followed by a string. These strings will appear in a new row (the next row currently)
C: changed. c can be followed by strings. These strings can replace rows between n1 and n2.
D: delete. d does not accept anything.
I: insert, I can be followed by strings, and these strings will appear in the new line (the previous line currently)
P: print, indicating that the selected data is printed. It usually appears with parameter-n.
S: replace, which can be directly replaced. sed's/string to be replaced/New String/flags. Here, the replace flag flags include:
G: indicates global replacement within the row. If g is not used, it is often replaced only when it appears for the first time.
A number between n: 1-, indicating to replace the nth occurrence of the specified mode in this mode
P: print the content of the mode space.
W: Write the content of the mode space to the file.
Q: end or exit sed.
Example:
1. delete a row
A. sed '2d 'file # Delete the second row
B. sed '$ d' file # Delete the last row
C. sed '1, 5d 'file # Delete the first row to the fifth row
D. sed '5, $ d' file # Delete the fifth row to the last row
E. sed '/My/,/You/d' file # Delete rows containing "My" to rows containing "You"
F. sed '/My/, 10d' file # Delete the content from the row containing "My" to the tenth line
G. sed '/^ $/d' file # Delete empty rows
2. display a row
A. sed-n '2p' file # display the second line
B. sed-n' $ P' file # display the last row
C. sed-n '1, 5' file # display the first row to the fifth row
D. sed-n'5, $ P' file # display the fifth row to the last row
3. query in Mode
A. sed-n'/sed/P' file # query all rows including the keyword sed
B. sed-n'/\ $/P' file # query all rows with the keyword $. Use "\" to block special meanings.
4. Add one or more strings
A. sed '1a test' file # Add the string "test" after the first line"
B. sed '1, 3a test' file # Add the string "test" after the first row to the third row"
C. sed '1a test \ ntest' file # add multiple lines after the first line and use the linefeed \ n
5. replace a part of one or more rows or one row.
A. sed '1c test' file # Replace the content in the first line with test
B. sed '1, 2c test' file # Replace the content from the first row to the second row with test
C. sed '1, 5S/a/A/'file # Replace the first a in the first to fifth with
D. sed '/B/s/a/A/G' file # Replace a with A in the row with B
6. insert
A. sed-I '$ a test' file # Enter test in the last line.
7. Multi-Point editing
A. sed-e '3, $ d'-e's/Test/test' file # first Delete the data from the third row to the last row, and then replace "test" with "Test ", $ needs to be separated from the regular expression $, which indicates the end of the row
B. sed-e '3, $ d; s/Test/test' file # same effect as above
8. Script
A. Run the following command to save it in a text file (sed-script)
3i \
~~~~~~~~~~~~~~~~~~~~~
3, $ s/\ (hrwang \) is \ (mjfan \)/\ 2 is \ 1/
$ \
We will love eachother forever !!
B. sed-f sed-script file
The above script can be further optimized
A. Keep the following command in a sh script (sed-script)
#! /Bin/sed-f
3i \
~~~~~~~~~~~~~~~~~~~~~
3, $ s/\ (hrwang \) is \ (mjfan \)/\ 2 is \ 1/
$ \
We will love eachother forever
B../sed-script file
9. Exit
A. sed '/^ name/Q' file # After matching, sed will output this line and then exit
B. sed '2q' file # exit after the second row is output
C. sed '/name/{s/name/NAME/; q;}' file # contains name in a line, replace name with NAME, and then exit sed
Note:
1. The sed-I action will be directly modified to the original file, so use it with caution.
2. The sed-e action indicates multi-point editing.
3. The sed script is a series of sed commands written in the file. In the script, no extra space or text is required at the end of the command. If multiple commands exist in one line, separate them with semicolons. When executing the script, sed first copies the first line of the input file to the mode buffer, and then executes all the commands in the script. After each row is processed, sed copies the next row of the file to the mode buffer and executes all the commands in the script. When using the sed script, no quotation marks are used to ensure that the sed command is not interpreted by shell.
4. The sed command can specify zero, one, or two addresses. Each address is a regular expression that describes the pattern, line number, or line addressing symbol. If no address is specified, the command applies to each line. If there is only one address, the command applies to any line that matches the address. If two addresses are specified, then, the command is applied to the first line matching the first address and the line following it until the line matching the second address (including this line) can be viewed as an activation action, and regard the second address as a disabled action. If there is an exclamation mark (!) behind the address (!), Then the command is applied to all rows that do not match the address.
Use of row location
5. Differences between c and s in sed commands (c is changed directly, s is replaced)
Introduce shell variable in sed command
Shell programming in Linux -- basic usage of sed command
Sed for Unix text processing tools
Sed advanced usage
Sed command details and examples
This article permanently updates the link address: