sed命令

sed命令

  • 常用选项:

    • -n 不输出模式空间内容到屏幕,即不自动打印

    • -e 多点编辑

    • -f /PATH/SCRIPT_FILE 从指定文件中读取编辑脚本

    • -r 支持使用扩展正则表达式

    • -i.bak 备份文件并原处编辑

script格式:

‘地址命令’

地址格式:

1、不给地址:对全文进行处理

2、单地址:

#:指定的行,$:最后一行

/pattern/: 被此处模式所能够匹配到的每一行

3、地址范围:

#,#

#,+#

/pat1/,/pat2/

#./pat1/

4、步进:~

1~2 奇数行

2~2 偶数行

命令:

p 重复打印

d 删除模式空间匹配的行,并立即启用下一轮循环

a []text 在指定行后面追加文本,支持使用\n实现多行追加

i []text 在行前面插入文本

c []text 替换行为单位或多行文本

w /path/file 保存模式匹配的行至指定文件

r /path/file 读取指定文件的文本至模式空间中匹配到的行后

= 为模式空间中的行打印行号

! 模式空间中匹配行取反处理

s/pattern/string/修饰符 查找替换,支持使用其它分隔符,可以是其它形式:s@@@, s###

替换修饰符:

g 行内全局替换

p 显示替换成功的行

w /PATH/FILE 将替换成功的行保存至文件中

正则表达式(Regular Expression)
在学习sed前,首先了解RE的基本知识,大体上最基本也需要知道下面这些,如果不了解正则表达式,那么您将很难进阶IMG_256

引用:

  • 錨點(anchor) 用以標識 RE 於句子中的位置所在. 常見有: ^: 表示句首. 如 ^abc 表示以 abc 開首的句子. : 表 示 句 尾 . 如 a b c : 表示句尾. 如 abc :.abc 表示以 abc 結尾的句子. <: 表示詞首. 如 \ >: 表示詞尾. 如 abc> 表示以 abc 結尾的詞. - 修飾字符(modifier) 獨立表示時本身不具意義, 專門用以修改前一個 char. set 的出現次數. 常見有: : 表示前一個 char. set 的出現次數為 0 或多次. 如 abc 表示 a 與 c 之間可有 0 或多個 b 存在. ?: 表示前一個 char. set 的出現次數為 0 或 1 次. 如 ab?c 表示 a 與 c 之間可有 0 或 1 個 b 存在. +: 表示前一個 char. set 的出現次數為 1 或多次. 如 ab+c 表示 a 與 c 之間可有 1 或多個 b 存在. {n}: 表示前一個 char. set 的出現次數必須為 n 次. 如 ab{3,}c 表示 a 與 c 之間必須有 3 個 b 存在.{n,}: 表示前一個 char. set 的出現次數至少為 n 次. 如 ab{3,}c 表示 a 與 c 之間至少有 3 個 b 存在. {n,m}: 表示前一個 char. set 的出現次數為 n 到 m 次. 如 ab{3,5}c 表示 a 與 c 之間有 3 到 5 個 b 存在.

. : 匹配任意一个字符(1个)
.*:匹配任意多个字符(1或多个)

查看/etc/issue,相当于cat /etc/issue

[root@VM-0-13-centos ~]# sed '' /etc/issue
\S
Kernel \r on an \m

[root@VM-0-13-centos ~]# sed 'p' /etc/issue
\S
\S
Kernel \r on an \m
Kernel \r on an \m


打印第1行

[root@VM-0-13-centos ~]# sed -n '1p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@VM-0-13-centos ~]# sed ''
a
a
b
b
cjz
cjz

打印第二行

[root@VM-0-13-centos ~]# ifconfig eth0 | sed -n '2p' 
inet 172.17.0.13 netmask 255.255.240.0 broadcast 172.17.15.255

打印最后一行

[root@VM-0-13-centos ~]# sed -n '$p' /etc/passwd 
cjz:x:1000:1000::/home/cjz:/bin/bash

打印root开头行

[root@VM-0-13-centos ~]# sed -n '/^root/p' /etc/passwd 
root:x:0:0:root:/root:/bin/bash

打印/dev/vd开头的行

[root@VM-0-13-centos ~]# df | sed -n '/^\/dev\/vd/p'
/dev/vda1 51473868 3567036 45709588 8% /

打印网卡netmask的行

[root@VM-0-13-centos ~]# ifconfig eth0 | sed -n '/netmask/p'
inet 172.17.0.13 netmask 255.255.240.0 broadcast 172.17.15.255

打印第3行到第6行

[root@VM-0-13-centos ~]# seq 10 | sed -n '3,6p'
3
4
5
6

打印第3行开始加后面4行

[root@VM-0-13-centos ~]# seq 10 | sed -n '3,+4p'
3
4
5
6
7

打印3到最后一行

[root@VM-0-13-centos ~]# seq 10 | sed -n '3,$p'
3
4
5
6
7
8
9
10

打印s开头到c开头的行

[root@VM-0-13-centos ~]# sed -n '/^s/,/^c/p' /etc/passwd
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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
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
libstoragemgmt:x:998:997:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
syslog:x:996:994::/home/syslog:/bin/false
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
tomcat:x:53:53:Apache Tomcat:/usr/share/tomcat:/sbin/nologin
cjz:x:1000:1000::/home/cjz:/bin/bash

打印19/Mar/2021:22:47:01至19/Mar/2021:23:49:15的行

[root@VM-0-13-centos ~]# sed -n '/19\/Mar\/2021:22:47:01/,/19\/Mar\/2021:23:49:15/p' /var/log/httpd/access_log
101.251.242.238 - - [19/Mar/2021:22:47:01 +0800] "\x16\x03\x01\x01\"\x01" 400 226 "-" "-"
101.251.242.238 - - [19/Mar/2021:22:47:01 +0800] "\x16\x03\x01\x01\"\x01" 400 226 "-" "-"
101.251.242.238 - - [19/Mar/2021:22:47:01 +0800] "\x16\x03\x01" 400 226 "-" "-"
101.251.242.238 - - [19/Mar/2021:22:47:01 +0800] "\x16\x03\x01" 400 226 "-" "-"
101.251.242.238 - - [19/Mar/2021:22:47:02 +0800] "GET / HTTP/1.1" 200 9 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0"
139.162.145.250 - - [19/Mar/2021:23:49:15 +0800] "\x16\x03\x01" 400 226 "-" "-"

打印奇数行

[root@VM-0-13-centos ~]# seq 10 | sed -n '1~2p' 
1
3
5
7
9

打印偶数行

[root@VM-0-13-centos ~]# seq 10 | sed -n '2~2p' 
2
4
6
8
10

删除偶数的行

[root@VM-0-13-centos ~]# seq 10 | sed '2~2d'
1
3
5
7
9

删除奇数的行

[root@VM-0-13-centos ~]# seq 10 | sed '1~2d'
2
4
6
8
10

删除第2行和第4行,两种方法

方法一:
[root@VM-0-13-centos data]# sed -e '2d' -e '4d' seq.log 
1
3
5
6
7
8
9
10
方法二:
[root@VM-0-13-centos data]# sed '2d;4d' seq.log 
1
3
5
6
7
8
9
10

先备份,在原文件删除第2行和第4行

[root@VM-0-13-centos data]# sed -i.bak '2d;4d' seq.log 
[root@VM-0-13-centos data]# cat seq.log
1
3
5
6
7
8
9
10

[root@VM-0-13-centos data]# cat seq.log.bak
1
2
3
4
5
6
7
8
9
10

在奇数行后一行加上+++++++

[root@VM-0-13-centos data]# sed '1~2a+++++++' seq.log
1
+++++++
2
3
+++++++
4
5
+++++++
6
7
+++++++
8
9
+++++++
10

1开头行加new line

[root@VM-0-13-centos data]# sed '/^1/a new line' seq.log 
1
new line
2
3
4
5
6
7
8
9
10
new line
[root@VM-0-13-centos ~]# sed '/User specific/a alias hahaha=ls' .bashrc 
# .bashrc

# User specific aliases and functions
alias hahaha=ls

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

i []text 在行前面插入文本

[root@VM-0-13-centos ~]# sed '/User specific/i alias hahaha=ls' .bashrc 
# .bashrc

alias hahaha=ls
# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

c []text 替换行为单位或多行文本

[root@VM-0-13-centos ~]# sed '/User specific/c alias hahaha=ls' .bashrc 
# .bashrc

alias hahaha=ls

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

在Listen 80的行后面追加listen 8080

[root@VM-0-13-centos ~]# sed -i.bak '/^Listen 80/a listen 8080' /etc/httpd/conf/httpd.conf 
[root@VM-0-13-centos ~]# ll /etc/httpd/conf/httpd.conf.bak 
-rw-r--r-- 1 root root 11754 Mar 11 00:39 /etc/httpd/conf/httpd.conf.bak
[root@VM-0-13-centos ~]# systemctl restart httpd
[root@VM-0-13-centos ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port 
LISTEN 0 50 *:3306 *:* 
LISTEN 0 128 *:22 *:* 
LISTEN 0 100 127.0.0.1:25 *:* 
LISTEN 0 128 [::]:8080 [::]:* 
LISTEN 0 128 [::]:80 [::]:* 
LISTEN 0 100 [::1]:25 

在 Listen 80后面追加两行listen 8080和listen 8081(\n换行符号)

[root@localhost ~]# sed -i '/^Listen 80/a listen 8080\nlisten 8081' /etc/httpd/conf/httpd.conf 

r /path/file 读取指定文件的文本至模式空间中匹配到的行后

[root@centos8 ~]#seq 10 | sed '1~2r /etc/issue'
1
\S
Kernel \r on an \m

2
3
\S
Kernel \r on an \m

4
5
\S
Kernel \r on an \m

6
7
\S
Kernel \r on an \m

8
9
\S
Kernel \r on an \m

10

s开头到c开头的行保存到f1文件,w /path/file 保存模式匹配的行至指定文件

[root@centos8 ~]#sed -n '/^s/,/^c/w f1' /etc/passwd
[root@centos8 ~]#cat f1
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
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
polkitd:x:998:996:User for polkitd:/:/sbin/nologin
geoclue:x:997:995:User for geoclue:/var/lib/geoclue:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pipewire:x:996:992:PipeWire System Daemon:/var/run/pipewire:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
libstoragemgmt:x:995:989:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
unbound:x:994:988:Unbound DNS resolver:/etc/unbound:/sbin/nologin
gluster:x:993:987:GlusterFS daemons:/run/gluster:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
saslauth:x:992:76:Saslauthd user:/run/saslauthd:/sbin/nologin
dnsmasq:x:985:985:Dnsmasq DHCP and DNS server:/var/lib/dnsmasq:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
sssd:x:984:984:User for sssd:/:/sbin/nologin
cockpit-ws:x:983:982:User for cockpit web service:/nonexisting:/sbin/nologin
setroubleshoot:x:979:978::/var/lib/setroubleshoot:/sbin/nologin
flatpak:x:978:977:User for flatpak system helper:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
clevis:x:977:976:Clevis Decryption Framework unprivileged user:/var/cache/clevis:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
a:x:1000:1000:a:/home/a:/bin/bash
nginx:x:975:974::/home/nginx:/sbin/nologin
chenjz:x:1001:1001::/home/chenjz:/bin/bash

= 为模式空间中的行打印行号

[root@localhost ~]# sed -n '/^#/=' /etc/fstab 
2
3
4
5
6
7
8
[root@localhost ~]# sed -n '/^#/!p' /etc/fstab 

UUID=9edb93ad-4755-4c44-adfe-04ae78aec136 / xfs defaults 0 0
UUID=5a32c480-61a0-48bb-a77a-63b02a1bdfce /boot xfs defaults 0 0
UUID=3037b05a-fbe0-44f9-9ee5-19b0543af505 /home xfs defaults 0 0
UUID=5a5b4003-712b-4ea8-b50e-41aa54f9d029 swap swap defaults 0 0
[root@localhost ~]# sed -i '/^#/d' fstab 
[root@localhost ~]# cat fstab 

UUID=9edb93ad-4755-4c44-adfe-04ae78aec136 / xfs defaults 0 0
UUID=5a32c480-61a0-48bb-a77a-63b02a1bdfce /boot xfs defaults 0 0
UUID=3037b05a-fbe0-44f9-9ee5-19b0543af505 /home xfs defaults 0 0
UUID=5a5b4003-712b-4ea8-b50e-41aa54f9d029 swap swap defaults 0 0

[root@localhost ~]# sed -n '/^#/!p' fstab 

UUID=9edb93ad-4755-4c44-adfe-04ae78aec136 / xfs defaults 0 0
UUID=5a32c480-61a0-48bb-a77a-63b02a1bdfce /boot xfs defaults 0 0
UUID=3037b05a-fbe0-44f9-9ee5-19b0543af505 /home xfs defaults 0 0
UUID=5a5b4003-712b-4ea8-b50e-41aa54f9d029 swap swap defaults 0 0

sed命令高级用法

[root@VM-0-13-centos ~]# sed -n 's/root/admin/gp' passwd 

admin:x:0:0:admin:/admin:/bin/bash

operator:x:11:0:operator:/admin:/sbin/nologin

[root@VM-0-13-centos ~]# sed -n '/r..t/p' passwd 

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

tomcat:x:53:53:Apache Tomcat:/usr/share/tomcat:/sbin/nologin

 

取IP

[root@localhost ~]# ifconfig ens33 | sed -n '2s/^.*inet //p' | sed -n 's/ netmask.*//p'

192.168.40.128 

[root@localhost ~]# ifconfig ens33 | sed -n '2s/^.*inet //;s/ netmask.*//p'

192.168.40.128 

[root@VM-0-13-centos ~]# ifconfig eth0 | sed -rn '2s/(.*inet )([0-9].*)(netmask.*)/\2/p'

172.17.0.13

centos7,8修改网卡名称

[root@localhost ~]# sed -ri '/^GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub

[root@localhost ~]# grub2-mkconfig -o /boot/grub2/grub.cfg

[root@localhost ~]# reboot

Ubuntu修改网卡名称

[root@localhost ~]# sed -ri '/^GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub

[root@localhost ~]# grub-mkconfig -o /boot/grub/grub.cfg

[root@localhost ~]# reboot

给所有非#开头的行加#

[root@localhost ~]# sed -rn 's/^[^#](.*)/# \1/p' /etc/fstab 
# UID=9edb93ad-4755-4c44-adfe-04ae78aec136 / xfs defaults 0 0
# UID=5a32c480-61a0-48bb-a77a-63b02a1bdfce /boot xfs defaults 0 0
# UID=3037b05a-fbe0-44f9-9ee5-19b0543af505 /home xfs defaults 0 0
# UID=5a5b4003-712b-4ea8-b50e-41aa54f9d029 swap swap defaults 0 0
[root@localhost ~]# sed -rn '/^#/!s@^@#@p' /etc/fstab 
#
#UUID=9edb93ad-4755-4c44-adfe-04ae78aec136 / xfs defaults 0 0
#UUID=5a32c480-61a0-48bb-a77a-63b02a1bdfce /boot xfs defaults 0 0
#UUID=3037b05a-fbe0-44f9-9ee5-19b0543af505 /home xfs defaults 0 0
#UUID=5a5b4003-712b-4ea8-b50e-41aa54f9d029 swap swap defaults 0 0

把#号开头的行去掉#号

[root@centos8 ~]#sed -rn '/^#/s/^#//p' /etc/fstab

 /etc/fstab
 Created by anaconda on Wed Oct 27 04:43:12 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.

[root@centos8 ~]#sed -ri.bak '/^#/s/^#//' /etc/fstab

取基名和目录名

#取目录名
[root@localhost ~]# echo /etc/sysconfig/ | sed -rn 's#(.*)/([^/]+/?)#\1#p'
/etc

[root@localhost ~]# echo /etc/sysconfig/ | sed -rn 's#(.*)/([^/]+/?)#\2#p'
sysconfig/

#取基明
[root@localhost ~]# echo /etc/sysconfig/ | sed -rn 's#(.*)/([^/]+)/?#\2#p'
sysconfig

 

 

[root@localhost network-scripts]# seq 10 | sed -n 'n;p'
2
4
6
8
10

[root@localhost network-scripts]# seq 10 | sed '1!G;h;$!d'
10
9
8
7
6
5
4
3
2
1

[root@localhost network-scripts]# seq 10 | sed '3h;9G;9!d'
9
3

批量改名:

[root@centos8 test]# touch 1.txt 2.log 3.pdf
[root@centos8 test]# ls
1.txt 2.log 3.pdf

[root@centos8 test]# touch 4.html
[root@centos8 test]# ls
1.txt 2.log 3.pdf 4.html

sed分组

[root@centos8 ~]# echo 1.2.log | sed -nr 's/(.*)\.([^.]+)$/\1/p'  
1.2

[root@centos8 ~]# echo 1.2.log | sed -nr 's/(.*)\.([^.]+)$/\2/p'
log
[root@centos8 test]# vim /data/script/for_rename.sh
#!/bin/bash
DIR=/data/test
cd $DIR
for FILE in * ;do
PRE=`echo $FILE | sed -nr 's/(.*)\.([^.]+)$/\1/p'`  #取前缀
#SUFFIX=`echo $FILE | sed -nr 's/(.*)\.([^.]+)$/\2/p'` #取后缀
 mv $FILE $PRE.bak
done

[root@centos8 test]# bash -n /data/script/for_rename.sh

[root@centos8 test]# bash /data/script/for_rename.sh 
[root@centos8 test]# ls
1.bak 2.bak 3.bak 4.bak

批量改名:

[root@centos8 test]# touch 1.txt 2.log 3.pdf
[root@centos8 test]# ls
1.txt 2.log 3.pdf

[root@centos8 test]# touch 4.html
[root@centos8 test]# ls
1.txt 2.log 3.pdf 4.html

sed分组

[root@centos8 ~]# echo 1.2.log | sed -nr 's/(.*)\.([^.]+)$/\1/p'  
1.2

[root@centos8 ~]# echo 1.2.log | sed -nr 's/(.*)\.([^.]+)$/\2/p'
log
[root@centos8 test]# vim /data/script/for_rename.sh
#!/bin/bash
DIR=/data/test
cd $DIR
for FILE in * ;do
PRE=`echo $FILE | sed -nr 's/(.*)\.([^.]+)$/\1/p'`  #取前缀
#SUFFIX=`echo $FILE | sed -nr 's/(.*)\.([^.]+)$/\2/p'` #取后缀
 mv $FILE $PRE.bak
done

[root@centos8 test]# bash -n /data/script/for_rename.sh

[root@centos8 test]# bash /data/script/for_rename.sh 
[root@centos8 test]# ls
1.bak 2.bak 3.bak 4.bak
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值