25.第五章 文本处理三剑客之 sed(一)
作者:互联网
5.文本处理三剑客之 sed
5.1 sed 工作原理
sed 即 Stream EDitor,和 vi 不同,sed是行编辑器
Sed是从文件或管道中读取一行,处理一行,输出一行;再读取一行,再处理一行,再输出一行,直到最后一行。每当处理一行时,把当前处理的行存储在临时缓冲区中,称为模式空间(Pattern Space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。一次处理一行的设计模式使得sed性能很高,sed在读取大文件时不会出现卡顿的现象。如果使用vi命令打开几十M上百M的文件,明显会出现有卡顿的现象,这是因为vi命令打开文件是一次性将文件加载到内存,然后再打开。Sed就避免了这种情况,一行一行的处理,打开速度非常快,执行速度也很快
参考网站:http://www.gnu.org/software/sed/manual/sed.html
5.2 sed 基本用法
格式:
sed [option]... 'script;script;...' inputfile...
常用选项:
-n 不输出模式空间内容到屏幕,即不自动打印
-e 多点编辑
-f FILE 从指定文件中读取编辑脚本
-r, -E 使用扩展正则表达式
-i.bak 备份文件并原处编辑
#说明:
-ir 不支持
-i -r 支持
-ri 支持
-ni 会清空文件
script格式:
'地址命令'
地址格式:
1. 不给地址:对全文进行处理
2. 单地址:
#:指定第#行,$:最后一行
/pattern/:被此处模式(正则表达式)所能够匹配到的每一行
3. 地址范围:
#,# #从#行到第#行,3,6 从第3行到第6行
#,+# #从#行到+#行,3,+4 表示从3行到第7行
/pat1/,/pat2/
#,/pat/
4. 步进:~
1~2 奇数行
2~2 偶数行
命令:
p 打印当前模式空间内容,追加到默认输出之后
Ip 忽略大小写输出
d 删除模式空间匹配的行,并立即启用下一轮循环
! 模式空间中匹配行取反处理
a [\]text 在指定行后面追加文本,支持使用\n实现多行追加
i [\]text 在行前面插入文本
c [\]text 替换行为单行或多行文本
w file 保存模式匹配的行至指定文件
r file 读取指定文件的文本至模式空间中匹配到的行后
= 为模式空间中的行打印行号
搜索替代
s/pattern/string/修饰符 查找替换,支持使用其它分隔符,可以是其它形式:s@@@,s###替换修饰符:
g 行内全局替换
p 显示替换成功的行
w /PATH/FILE 将替换成功的行保存至文件中
I,i 忽略大小写
范例:
[root@rocky8 ~]# sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
-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
specify the desired line-wrap length for the `l' command
--posix
disable all GNU extensions.
-E, -r, --regexp-extended
use extended regular expressions in the script
(for portability use POSIX -E).
-s, --separate
consider files as separate rather than as a single,
continuous long stream.
--sandbox
operate in sandbox mode (disable e/r/w commands).
-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
If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret. All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.
GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.
[root@rocky8 ~]# sed ''
raymond
raymond
boss
boss
linux
linux
^C
#sed默认功能就是读入一行,显示一行,它就是把每一次读入的标准输入放入到内存中,放到模式空间,默认把读入的行进行显示,叫自动打印
#不给地址:对全文进行处理
[root@rocky8 ~]# seq 10 |sed ''
1
2
3
4
5
6
7
8
9
10
#既然支持标准输入,可以用管道进行标准输入
[root@rocky8 ~]# seq 10 |sed 'p' #p 打印
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
[root@rocky8 ~]# seq 10 |sed -n 'p' #-n 关闭自动打印
1
2
3
4
5
6
7
8
9
10
[root@rocky8 ~]# seq 10 |sed -n '3p'
3
#指定打印行
[root@rocky8 ~]# seq 10 |sed -n '$p'
10
#$ 打印最后一行
#下面方法都是错误的
[root@rocky8 ~]# seq 10 |sed -n '$-1p'
sed: -e expression #1, char 2: unknown command: `-'
[root@rocky8 ~]# seq 10 |sed -n '($-1)p'
sed: -e expression #1, char 1: unknown command: `('
[root@rocky8 ~]# seq 10 |sed -n '\($-1\)p'
sed: -e expression #1, char 8: unterminated address regex
[root@rocky8 ~]# seq 10 |sed -en '\($-1\)p'
sed: can't read \($-1\)p: No such file or directory
[root@rocky8 ~]# seq 10 |sed -n '2p'
2
[root@rocky8 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.31.1.8 netmask 255.255.248.0 broadcast 172.31.7.255
inet6 fe80::20c:29ff:fef9:6ad1 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:f9:6a:d1 txqueuelen 1000 (Ethernet)
RX packets 2753350 bytes 4032564562 (3.7 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 387850 bytes 52488253 (50.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@rocky8 ~]# ifconfig |sed -n '2p'
inet 172.31.1.8 netmask 255.255.248.0 broadcast 172.31.7.255
[root@rocky8 ~]# ifconfig eth0 | sed -n '/netmask/p'
inet 172.31.1.8 netmask 255.255.248.0 broadcast 172.31.7.255
#支持正则表达式
[root@rocky8 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Oct 6 11:16:34 2021
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
[root@rocky8 ~]# sed -n '/^UUID/p' /etc/fstab #显示UUID开头的行
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
[root@rocky8 ~]# sed -n '/[^#]/p' /etc/fstab #过滤掉空行
# /etc/fstab
# Created by anaconda on Wed Oct 6 11:16:34 2021
# 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.
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
[root@rocky8 ~]# sed -n '/^[^#]/p' /etc/fstab #过滤掉#开头行和空行
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
[root@rocky8 ~]# sed -n '/^bin/,/^mail/p' /etc/passwd #显示bin开头到mail开头的行
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
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[root@rocky8 ~]# sed -n '5,/^mail/p' /etc/passwd #显示第5行到mail开头的行
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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[root@rocky8 ~]# seq 10 | sed -n '3,6p' #从第3行到第6行显示
3
4
5
6
[root@rocky8 ~]# seq 10 | sed -n '3,+4p' #从第三行往后加4行显示
3
4
5
6
7
[root@rocky8 ~]# seq 10 | sed -n '3,$p' #从第3行到最后一行显示
3
4
5
6
7
8
9
10
[root@rocky8 ~]# seq 10 |sed -n '1~2p' #1~2 奇数行
1
3
5
7
9
[root@rocky8 ~]# seq 10 |sed -n '2~2p' #2~2 偶数行
2
4
6
8
10
[root@rocky8 ~]# seq 10 |sed '1~2d' #删除奇数行
2
4
6
8
10
[root@rocky8 ~]# seq 10 |sed '2~2d' #删除偶数行
1
3
5
7
9
[root@rocky8 ~]# seq 10|sed '1,5d' #删除1-5行
6
7
8
9
10
[root@rocky8 ~]# sed -n '/^mail/p' /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[root@rocky8 ~]# sed -n '/^MAIL/Ip' /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
#I 忽略大小写
[root@rocky8 ~]# sed '/UUID/d' /etc/fstab #删除UUID开头的行
#
# /etc/fstab
# Created by anaconda on Wed Oct 6 11:16:34 2021
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
#d 删除包含的行
[root@rocky8 ~]# sed '1d' /etc/fstab #删除第一行
#
# /etc/fstab
# Created by anaconda on Wed Oct 6 11:16:34 2021
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
[root@rocky8 ~]# sed -n '/^#/!p' /etc/fstab #过滤掉#开头的行
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
# ! 取反,排除
[root@rocky8 ~]# yum -y install httpd
[root@rocky8 ~]# sed -n '/^[^#]/p' /etc/httpd/conf/httpd.conf #过滤掉#开头的行和空行
[root@rocky8 ~]# sed -n '/^ *#/!p' /etc/httpd/conf/httpd.conf #过滤掉#前面有空格的行
#过滤掉#开头、空行、和空白字符后有#的行
[root@rocky8 ~]# sed -n '/^$/d;/^[[:space:]]*#/!p' /etc/httpd/conf/httpd.conf
#过滤掉#开头和空行
[root@rocky8 ~]# sed -r '/^(#|$)/d' /etc/httpd/conf/httpd.conf #-r 支持扩展正则表达式
[root@rocky8 ~]# sed '/UUID/a linux' /etc/fstab #在UUID 的下一行插入linux
#
# /etc/fstab
# Created by anaconda on Wed Oct 6 11:16:34 2021
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
linux
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
linux
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
linux
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
linux
[root@rocky8 ~]# sed '/UUID/a\ line1\n line2' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Oct 6 11:16:34 2021
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
line1
line2
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
line1
line2
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
line1
line2
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
line1
line2
#第一行想要缩进,a 后面加个斜线 ,斜线表示开始
[root@rocky8 ~]# sed '/UUID/w /data/a.txt' /etc/fstab #把UUID开头的行保存到/data/a.txt文件
#
# /etc/fstab
# Created by anaconda on Wed Oct 6 11:16:34 2021
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
[root@rocky8 ~]# ll /data/a.txt
-rw-r--r-- 1 root root 376 Oct 11 20:03 /data/a.txt
[root@rocky8 ~]# cat /data/a.txt
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
# w 找到 保存至文件
[root@rocky8 ~]# sed '/UUID/r /etc/issue' /etc/fstab #在UUID的行下方读入文件/etc/issue
#
# /etc/fstab
# Created by anaconda on Wed Oct 6 11:16:34 2021
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
\S
Kernel \r on an \m
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
\S
Kernel \r on an \m
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
\S
Kernel \r on an \m
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
\S
Kernel \r on an \m
#r 把文件读入
[root@rocky8 ~]# sed '/UUID/=' /etc/fstab #在UUID的行前面加行号
#
# /etc/fstab
# Created by anaconda on Wed Oct 6 11:16:34 2021
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
12
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
13
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
14
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
15
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
#= 等号,加行号
[root@rocky8 ~]# cp /etc/fstab .
[root@rocky8 ~]# sed 's/UUID/uuid/' fstab #把UUID替换成uuid
#
# /etc/fstab
# Created by anaconda on Wed Oct 6 11:16:34 2021
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
uuid=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
uuid=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
uuid=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
uuid=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
[root@rocky8 ~]# sed -n 's/UUID/uuid/p' fstab
uuid=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
uuid=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
uuid=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
uuid=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
[root@rocky8 ~]# seq 10 |sed -n -e '2p'
2
[root@rocky8 ~]# seq 10 |sed -n -e '2p' -e '6p' #可以两个-e单独分开写
2
6
[root@rocky8 ~]# seq 10 |sed -n -e '2p;6p' #也可以两个写一块用; 分号隔开
2
6
#-e 多点编辑
[root@rocky8 ~]# echo 2p >sed.txt
[root@rocky8 ~]# cat sed.txt
2p
[root@rocky8 ~]# seq 10 |sed -n -f sed.txt
2
#也可以把脚本写入文件,直接调用
范例:
[root@rocky8 ~]# sed ''
rocky
rocky
linux
linux
^C
[root@rocky8 ~]# sed '' /etc/issue
\S
Kernel \r on an \m
[root@rocky8 ~]# sed 'p' /etc/issue
\S
\S
Kernel \r on an \m
Kernel \r on an \m
[root@rocky8 ~]# sed -n '' /etc/issue
[root@rocky8 ~]# sed -n 'p' /etc/issue
\S
Kernel \r on an \m
[root@rocky8 ~]# sed -n '1p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@rocky8 ~]# ifconfig eth0 | sed '2p'
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.31.1.8 netmask 255.255.248.0 broadcast 172.31.7.255
inet 172.31.1.8 netmask 255.255.248.0 broadcast 172.31.7.255
inet6 fe80::20c:29ff:fef9:6ad1 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:f9:6a:d1 txqueuelen 1000 (Ethernet)
RX packets 2762453 bytes 4043476630 (3.7 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 390407 bytes 52879750 (50.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@rocky8 ~]# ifconfig eth0 | sed -n '2p'
inet 172.31.1.8 netmask 255.255.248.0 broadcast 172.31.7.255
[root@rocky8 ~]# sed -n '$p' /etc/passwd
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
[root@rocky8 ~]# ifconfig eth0 |sed -n '/netmask/p'
inet 172.31.1.8 netmask 255.255.248.0 broadcast 172.31.7.25
[root@rocky8 ~]# df | sed -n '/^\/dev\/sd/p'
/dev/sda2 104806400 4015200 100791200 4% /
/dev/sda3 52403200 440948 51962252 1% /data
/dev/sda1 1038336 191796 846540 19% /boot
[root@rocky8 ~]# seq 10 > seq.log
[root@rocky8 ~]# sed -e '2d' -e '4d' seq.log
1
3
5
6
7
8
9
10
[root@rocky8 ~]# sed '2d;4d' seq.log
1
3
5
6
7
8
9
10
[root@rocky8 ~]# sed -i.orig '2d;4d' seq.log
[root@rocky8 ~]# cat seq.log.orig
1
2
3
4
5
6
7
8
9
10
[root@rocky8 ~]# cat seq.log
1
3
5
6
7
8
9
10
[root@rocky8 ~]# seq 10 > seq.log
[root@rocky8 ~]# sed -i.orig '2d;4d' seq.log
[root@rocky8 ~]# sed -i '/^listen 9527/a listen 80 \nlisten 8080' /etc/httpd/conf/httpd.conf
#删除所有以#开头的行
[root@rocky8 ~]# sed -i '/^#/d' fstab
#只显示非#开头的行
[root@rocky8 ~]# sed -n '/^#/!p' fstab
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 / xfs defaults 0 0
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot xfs defaults 0 0
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data xfs defaults 0 0
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none swap defaults 0 0
#修改网卡配置
[root@rocky8 ~]# sed -Ei.bak '/^GRUB_CMDLINE_LINUX/s/(.*)(")$/\1net.ifnames=0\2/' /etc/default/grub
标签:25,UUID,文本处理,sed,defaults,xfs,root,rocky8,三剑客 来源: https://blog.csdn.net/qq_25599925/article/details/120810135