Sed
1. Command introduction
First go to the help documentation to view the description information of the command
NAME
sed - stream editor for filtering and transforming text
The sed command is a powerful tool for manipulating, filtering, and converting text content. Common functions include combining regular expressions to quickly add, delete, modify, and query files. Among the query functions, the two most commonly used functions are filtering (extracting strings) and fetching lines (retrieve the specified line) .
Tips: Note that both sed and awk use single quotes, and double quotes have special interpretations.
2. Grammatical format
The syntax format is: sed [parameter] [sed built-in command character] [file]
SYNOPSIS
sed [OPTION]… {script-only-if-no-other-script} [input-file]…
3. Basic parameters
-n, --quiet, --silent
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
--follow-symlinks
follow symlinks when processing in place
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
-c, --copy
use copy instead of rename when shuffling files in -i mode
-b, --binary
does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX ( open files in
binary mode (CR+LFs are not treated specially))
-l N, --line-length=N
--posix
disable all GNU extensions.
-r, --regexp-extended
use extended regular expressions in the script.
-s, --separate
consider files as separate rather than as a single continuous long stream.
-u, --unbuffered
load minimal amounts of data from the input files and flush the output buffers
more often
-z, --null-data
separate lines by NUL characters
--help
display this help and exit
--version
output version information and exit
The commonly used command parameters are as follows:
-n | Only output the matched content, often used together with the sed built-in command p |
-i | Write the modified result content directly to the file |
-e | edited multiple times |
-r | Support for regular expression extensions |
If the -i parameter is not added, it means that the sed command only executes this command in memory, and the information in the file will not change; after adding the -i parameter, the result will be written to the file and not output to the screen .
sed built-in command parameters:
a | Append content, add one or more lines of text after the specified line |
d | delete matching line |
i | Indicates inserting text, adding one or more lines before the specified line |
p | Print the content of the matched line, usually used with -n |
s/regular/replacement content/g | Match the regular content, then replace the content, the ending g means global match |
s/regularity/replacement content/g, the middle / can be replaced with any other characters, such as #, @, + and so on.
sed matches the range:
Empty address: Indicates full-text processing
Single address: Specify a certain line of the file /pattern/: The range of
each line matched by the pattern : 10,20 means 10 to 20 lines, 10,+5 means the content of 5 lines down from the 10th line,
/pattern1/,/pattern2/
step size: 1-2, means 1, 3, 5, 7, 9 base lines, 2-2 two steps, means 2, 4, 6, 8, 10 even lines
4. Reference case
4.1 Find file content
Find /etc/passwd
the contents of the second to fifth lines, -n p
indicating that the matching results will be output to the screen
[root@localhost ~]# sed -n "2,5p" /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Find /etc/passwd
the contents of the fifth line and the next three lines
[root@localhost ~]# sed -n "5,+3p" /etc/passwd
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
4.2 Find the content of keywords
Find and filter content with root characters in /etc/passwd
and print to the screen
[root@localhost ~]# sed "/root/p" /etc/passwd -n
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
4.3 Delete lines from a file
Delete the line with good, -i
the parameters needed here will be used, and you will see that the result is not output to the screen, but the result to be executed is directly written to the file.
[root@localhost ~]# sed "/good/d" test.txt -i
[root@localhost ~]# cat test.txt
appleeeeeee
bear
appdieee
dadaaaaaa
pperd
You can also delete the content from a line to the end
[root@localhost ~]# sed '3,$d' test.txt -i
[root@localhost ~]# cat test.txt
appleeeeeee
bear
If you want to delete the text from root to ftp, but you don't know the number of lines of text in the file, you can use regularization to perform this task.
[root@localhost ~]# sed '/^root/,/^ftp/d' passwd | head -10
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
host:x:1000:1000:host:/home/host:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
libstoragemgmt:x:998:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
#执行的结果到文件中
[root@localhost ~]# sed '/^root/,/^ftp/d' passwd -i
4.4 Replace text content
The parameters needed to replace the text content s///g
, of course, you can use this method to write the result to the file.
[root@localhost ~]# sed 's/bear/pear/g' test.txt -i
[root@localhost ~]# cat test.txt
appleeeeeee
pear
appleeeeeee
appleeeeeee
pear
pear
appleeeeeee
pear
[root@localhost ~]# sed 's#apple#door#g' test.txt
dooreeeeee
pear
dooreeeeee
dooreeeeee
pear
pear
dooreeeeee
pear
You can also replace the content of the text multiple times
[root@localhost ~]# sed -e 's/do/done/g' -e 's/pear/cat/g' test.txt -i
[root@localhost ~]# cat test.txt
appleeeeeee
cat
appleeeeeee
appleeeeeee
cat
cat
appleeeeeee
cat
4.5 Write content
Append content to the next line and write to the file
[root@localhost ~]# sed '3a hello world!' passwd -i
[root@localhost ~]# head -n 5 passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
hello world!
adm:x:3:4:adm:/var/adm:/sbin/nologin
If you want to add content after each line, you don't need to add numbers in front
[root@localhost ~]# sed 'a hello world' passwd -i
[root@localhost ~]# head -5 passwd
root:x:0:0:root:/root:/bin/bash
hello world
bin:x:1:1:bin:/bin:/sbin/nologin
hello world
You can also write two lines of content at the same time, use \n
to wrap
[root@localhost ~]# sed '5a this is passwd\nthis is test' passwd -i
[root@localhost ~]# sed '1a this is passwd\nthis is test' passwd -i
[root@localhost ~]# head -5 passwd
root:x:0:0:root:/root:/bin/bash
this is passwd
this is test
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
4.6 View the IP address of the network card
The use of -e here can also be said to use the method of pinching the head and removing the tail, using three kinds of sed interception.
[root@localhost ~]# ifconfig ens32 | sed '2p' -n | sed 's/^.*inet//' | sed 's/net.*$//'
192.168.10.24
[root@localhost ~]# ifconfig ens32 | sed -e '2s/^.*inet//' -e '2s/net.*$//p' -n
192.168.10.24
[root@localhost ~]# ifconfig ens32 |sed '2s/^.*inet//;s/netmask.*//p' -n
4.7 View configuration files
We want to check the content of the file that does not contain the # sign in the configuration file. Can this command also be used? You can delete the line with the # sign and the space, so that the configuration file you want to see is displayed Information.
[root@localhost ~]# sed -e '/#/d' -e '/^$/d' /etc/httpd/conf/httpd.conf
-e
It's okay to not use parameters, you can use a semicolon to write two matching information together.
[root@localhost ~]# sed -e '/#/d;/^$/d' /etc/httpd/conf/httpd.conf
4.8 Add # in front of the content of lines 1-3
Under normal circumstances, there is no space in front of the content. How to achieve this effect? In fact, here is also the need to use the replacement method to achieve.
Parameter expansion is used -r
to match the first character information into the first group, and the second group is written with #, and the \1
result of the previous group is used for matching.
[root@localhost ~]# sed -r -n '1,3s/(^.)/#\1/gp' passwd
#root:x:0:0:root:/root:/bin/bash
#bin:x:1:1:bin:/bin:/sbin/nologin
#daemon:x:2:2:daemon:/sbin:/sbin/nologin
4.9 Find out the system version
You can view the information of this file first
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
If I just want to check the specific version information, such as 7.9.2009, it will be easier to use awk.
[root@localhost ~]# awk '{print $4}' /etc/redhat-release
7.9.2009
If you want to use the sed command, how to use it, let's think about it first, can we use the method of cutting the head and tail like the previous interception of IP
[root@localhost ~]# sed -r -n 's/^.*ase[[:space:]]//p' /etc/redhat-release
7.9.2009 (Core)
Summarize
The function of sed is really powerful. It can be used to filter, convert text, or write information. You can choose to use this parameter command. If you think it’s okay, you can like it and support it!