Sed usage instructions, sed usage
Sed Introduction
Sed: stream editor
It is a row Editor, or a stream editor. Each time a row is processed, a row is processed and the next row is processed. Sed does not directly process the source file, but reads a row and puts it into the patten space. The mode space is edited and displayed. Of course, you can add the-I parameter to sed to directly process the file. Sed not only has the mode space, but also has the concept of space persistence. The mode space and the content of the space can exchange, overwrite, and append, but it is rarely used, it is also easy to use and belongs to the advanced usage of sed.
Sed usage
Sed [option]… 'Script' inputfile... ...
Common options:
-N: Content in non-output mode to the screen
-E: Multi-Point editing
-F/path/to/script_file: Read the editing script from the specified file.
-R: supports the use of extended Regular Expressions
-I: modify the original file
Address demarcation:
1. Do not give the address: process the full text
2. Single address:
#: Specified row
/Pattern/: each row that can be matched by this pattern
3. Address range:
#,#: Indicates the number of rows to the number of rows.
#, + #: Indicates the number of rows after the start of the row.
/Pat1/,/pat2/: indicates the line from matching to Pattern 1 to matching to Pattern 2.
Edit command:
D: Delete
P: Display
A \ text: append text to the end of a row. You can use \ n to append multiple rows.
I \ text: insert text before a row; Use \ n to insert multiple rows
C \ text: Replace the behavior line or multi-line text
W/path/to/somefile: Save the row that matches the space in the mode to the specified file.
R/path/from/somefile: after reading the text of the specified file and streaming it to the row of the matched row in the mode space
=: Print the row number for the row in the mode space
! : Reverse
S ///: Replacement mark, which is replaced globally in the s // g line. Other delimiters are supported, such as s, s ###
Example:
[root@slave ~]# cat ifcfg-ens33 TYPE="Ethernet"BOOTPROTO="none"DEFROUTE="yes"IPV4_FAILURE_FATAL="no"IPV6INIT="yes"IPV6_AUTOCONF="yes"IPV6_DEFROUTE="yes"IPV6_FAILURE_FATAL="no"IPV6_ADDR_GEN_MODE="stable-privacy"NAME="ens33"UUID="3503f57e-dce7-425e-870f-efbc4897bfb2"DEVICE="ens33"ONBOOT="yes"IPADDR="192.168.100.20"PREFIX="24"GATEWAY="192.168.100.2"DNS1="114.114.114.114"IPV6_PEERDNS="yes"IPV6_PEERROUTES="yes"IPV6_PRIVACY="no"
1. display the content of rows 1 to 3
[root@slave ~]# sed -n '1,3p' ifcfg-ens33 TYPE="Ethernet"BOOTPROTO="none"DEFROUTE="yes"
2. Delete row 2nd to the last row
[root@slave ~]# sed '2,$d' ifcfg-ens33 TYPE="Ethernet"
3. Replace 192.168.100.20 with 192.168.100.30 and only display this row.
[root@slave ~]# sed -n 's/192.168.100.20/192.168.100.30/;/IPADDR/p' ifcfg-ens33 IPADDR="192.168.100.30"
4. Change all IPv6.
[root@slave ~]# sed -n '1,$ s/IPV6/ipv6/g;/ipv6/p' ifcfg-ens33 ipv6INIT="yes"ipv6_AUTOCONF="yes"ipv6_DEFROUTE="yes"ipv6_FAILURE_FATAL="no"ipv6_ADDR_GEN_MODE="stable-privacy"ipv6_PEERDNS="yes"ipv6_PEERROUTES="yes"ipv6_PRIVACY="no"
5. Add the text "hello sed" before and after lines 1st, and display lines 1 to 4.
[root@slave ~]# sed -ne '1 i \hello sed\n===============' -e '3 a ===================\n\hello sed' -e '1,4p' ifcfg-ens33 hello sed===============TYPE="Ethernet"BOOTPROTO="none"DEFROUTE="yes"===================hello sedIPV4_FAILURE_FATAL="no"
6. Delete lines containing IPV6
[root@slave ~]# sed -e '/.*IPV6.*/d' ifcfg-ens33 TYPE="Ethernet"BOOTPROTO="none"DEFROUTE="yes"IPV4_FAILURE_FATAL="no"NAME="ens33"UUID="3503f57e-dce7-425e-870f-efbc4897bfb2"DEVICE="ens33"ONBOOT="yes"IPADDR="192.168.100.20"PREFIX="24"GATEWAY="192.168.100.2"DNS1="114.114.114.114"
7. when reading the sed script from the file sedscript, you must change BOOTPROTO = "none" to "static" to comment out all lines containing IPV6, add ===sed is a strong tool ==at the beginning and end of the file, change the gateway to 192.168.100.3, and save the modified content to the ifcfg-test file.
[root@slave sed]# cat sedscript #!/bin/sed -f# this is a sed script1 i \=== sed is strong tool === s/^IPV6/# IPV6/g s/none/static/s/192.168.100.2/192.168.100.3/$ a \=== sed is strong tool ===w ifcfg-test
[root@slave sed]# chmod +x sedscript [root@slave sed]# ./sedscript ifcfg-ens33
[root@slave sed]# cat ifcfg-testTYPE="Ethernet"BOOTPROTO="static"DEFROUTE="yes"IPV4_FAILURE_FATAL="no"# IPV6INIT="yes"# IPV6_AUTOCONF="yes"# IPV6_DEFROUTE="yes"# IPV6_FAILURE_FATAL="no"# IPV6_ADDR_GEN_MODE="stable-privacy"NAME="ens33"UUID="3503f57e-dce7-425e-870f-efbc4897bfb2"DEVICE="ens33"ONBOOT="yes"IPADDR="192.168.100.30"PREFIX="24"GATEWAY="192.168.100.3"DNS1="114.114.114.114"# IPV6_PEERDNS="yes"# IPV6_PEERROUTES="yes"# IPV6_PRIVACY="no"
The content inserted and appended by I and a in the above example is not written to the file, but can be redirected.