In sed processing, there are two buffers: [pattern space] and [hold space]
Sed execution process:
First read a line, remove the trailing newline character, store it in [pattern space], and execute the edit command. After processing, unless the – n parameter is added, the current [pattern space] will be printed out, and the line breaks that have been removed will be printed at the end. Leave [pattern space] blank. Then read the next line and process the next line.
The default output of SED: the content in [pattern space] is output to the standard output.
Common options:
-
The content in [pattern space] is not output to standard output: – n
-
By default, only one script can be executed, and multiple scripts can be executed: – e script, – expression = script
There can be multiple – e scripts
-
If there are too many scripts to execute, you can specify a script file: – F / path / to / sed_ scirpt
In the script file, there is an edit command for each line.
-
Support the use of extended regular expression, the default is basic regular expression: – R
-
Edit the original file directly: – I
Address delimitation:
1. Do not give address: process the full text
2. Single address
- #: specify the line
- /Pattern /: each row matched by this pattern
3. Address range
-
$: last line
- #Beginning and end
- #, + ා: start, and how many lines are added from the beginning
- #, / pat1 /: start to, line to which pat1 matches
-
/Pat1 /, / pat2 /: the row that pat1 matches, and the row to which pat2 matches
4. Step by step~
- 1~2:1,3,5,7,9.。。 Rows (all odd rows)
- 2~2:2,4,6,8,10.。。 Rows (all even rows)
Edit command:
If there are multiple commands, separate them with semicolons.
-
Delete the content in [pattern space]: D
Delete the first to fifth lines of / etc / fstab.
# sed '1,5d' /etc/fstab
-
Print the content in [pattern space]: P
At first glance it looks like odd lines are printed, but actually even lines are printed, and odd lines are printed twice. # sed '1~2p' /etc/fstab After using - n to disable sed's default behavior, it is time to print odd lines # sed -n '1~2p' /etc/fstab
-
Insert before line: – I / text supports multi line insertion using
# sed '3a \new line\nother line' /etc/fstab # # /etc/fstab new line other line Add a comment before the UUID line: # sed '/^UUID/i \#this is base for UUID' /etc/fstab #this is base for UUID UUID=3d3b316a-529e-484a-9895-e785fdde5365 /boot xfs defaults 0 0
-
Insert after line: – A / text supports multi line insertion using
# sed '3i \new line\nother line' /etc/fstab # new line other line # /etc/fstab
-
Replace line: – C \ \ text replace the matched line with text
Replace the line beginning with UUID with text.
# sed '/^UUID/c \#this is base for UUID' /etc/fstab
-
Save the matching line to the specified file: – w / path / to / save
Keep the lines that do not begin with “ා” to / TMP / fsnew
# sed -n '/^[^#]/w /tmp/fsnew' /etc/fstab
-
Insert the contents of the specified file after the matching line: – R / path / to / insert
After the third line of / etc / fstab, insert the contents of / TMP / TST.
# cat /tmp/tst aaa bbb [[email protected] ~]# sed '3r /tmp/tst' /etc/fstab # # /etc/fstab aaa bbb
-
Add the line number on the previous line of the matched line:=
Add a line number to the line that begins with UUID
# sed '/^UUID/=' /etc/fstab # # /etc/fstab # Created by anaconda on Fri Nov 29 16:44:28 2019 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/centos-root / xfs defaults 0 0 10 UUID=3d3b316a-529e-484a-9895-e785fdde5365 /boot xfs defaults 0 0
-
The matched line will not execute the following command; the unmatched line will execute the following command:!. Note: before processing the command.
Delete lines not beginning with ා:
# sed '/^#/!d' /etc/fstab # # /etc/fstab # Created by anaconda on Fri Nov 29 16:44:28 2019 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info #
-
Find replacement: S / to replace / replace with / replace tag. Its separator / can be specified by itself. The common ones are s @ @ @, S###
Replacement mark:
- Global substitution: G
- Save the successful replacement result to the file: w / path / to / save
- Display the lines with successful replacement: P
practice
1. Delete all white space characters at the beginning of all lines that start with white space characters in the test file
# cat test
11
222
333
\+: the number of times before matching is greater than or equal to 1
# sed '[email protected]^[[:space:]]\[email protected]@' test
11
222
333
# sed -n '[email protected]^[[:space:]]\[email protected]@p' test
11
333
2. Delete the “#” sign at the beginning of all lines beginning with “#” and all blank characters after “#” in the / etc / fstab file
# sed '[email protected]^#[[:space:]]*@@' /etc/fstab
3. Delete the “ා” sign at the beginning of the line and all blank characters after “#” in the / etc / fstab file, and delete all lines beginning with UUID
# sed -e '[email protected]^#[[:space:]]*@@' -e '/^UUID/d' /etc/fstab
4. Output an absolute path to the SED command and take out the directory part. Its behavior is similar to dirname
If there is / after the message, the matching fails.
# echo "/var/log/messages" | sed '[email protected][^/]\[email protected]@'
/var/log/
Even if there is / after message, it can match: not/
[^ /]: not/
\+: at least one
/\?: / has 0 or 1
# echo "/var/log/messages/" | sed '[email protected][^/]\+/\[email protected]@'
/var/log/
Even if there is / after message, it can match
Due to the use of - R (extended regular expression), + and? The preceding / can be omitted.
# echo "/var/log/messages/" | sed -r '[email protected][^/]+/[email protected]@'
/var/log/
[[email protected] ~]#
Advanced editing commands
- Cover the contents in the [pattern space] space to the [hold space] space: H
- Add the content in the [pattern space] space to the [hold space] space, and then delete the content in the [pattern space] space: H
- Cover the contents in the [hold space] space to the [pattern space] space: G
- Add the content in the [hold space] space to the [pattern space] space, and then delete the content in the [hold space] space: G
- Exchange the contents in [hold space] [pattern space]: X
- Put the next line of the matched line into [pattern space], and delete the matched line: n
- Put the next line of the matched line into [pattern space], and do not delete the matched line: n
- Delete the line in [pattern space]: D
- Delete all lines in [pattern space] in multi line mode. (for example, if n is used, there are multiple lines in [pattern space]): D
1, display even lines:
Execution process: first read the first line into [pattern space], then the command n. N means to read the next line, delete the content in the current [pattern space], and then put the content of the next line into [pattern space]; the next command is p, P is to output the content in the current [pattern space] to the standard output, and all the second lines will be printed to the standard output. Then read line 3, the following command is n, read another line, and so on.
# sed -n 'n;p' /etc/fstab
2. Inverted text:
see: https://blog.csdn.net/itsenlin/article/details/21129405
Execution process: first read the first line into [pattern space], and find that the command in the first line is 1! G, so it is not processed; then, h, the content of [pattern space] will be covered to [hold space]; then, the command $! D will be executed because it is not the last line, so execute the command d to delete the contents in [pattern space].
Read the second line to [pattern space], and find that the command is 1! G. since it is not the first line, execute the G command and append the content of hold space] to [pattern space]. At this time, the contents of the second line and the first line are placed in [pattern space], and the second line is in front of the first line. Next is $! D. since it is not the last line, execute the D command to delete the contents in [pattern space].
# cat test
11
222
333
In the last line, D is not executed, and the content in [pattern space] is not used, so the flashback is printed to standard output.
# sed '1!G;h;$!d' ./test
333
222
11
You can also use the following command to achieve inversion.
Since the D command is not used, there is always content in [pattern space], so you must use – N. finally, use the P command to output the content in [pattern space] to the standard output.
# sed -n '1!G;h;$p' test
3. Take out the last line:
# sed '$!d' /etc/fstab
4. Take out the last two lines:
# sed '$!N;$!D' test
5. Delete all the original blank lines, and then add a blank line for all non blank lines
# cat test
11
Empty line
Empty line
222
Empty line
Empty line
333
444
555
666
# sed '/^$/d;G' test
11
Empty line
222
Empty line
333
Empty line
444
Empty line
555
Empty line
666
Empty line
6, display odd lines
# sed 'n;d' /etc/fstab
7. Add a blank line after each line
# sed 'G' /etc/fstab
8. Practice the usage of H and G
# cat t2
one
two
three
# sed 'H;g' t2
A blank line
one
A blank line
one
two
A blank line
one
two
three
Illustration: H is added to hold space,In hold space, there is a line break. So the first time you add one to hold space, the line before one is empty.