Sed 命令

sed工具

 用法:
sed [option]... 'script' inputfile...

常用選項:
-n 不輸出模式空間內容到屏幕,即不自動打印
-e 多點編輯
-f /PATH/SCRIPT_FILE 從指定文件中讀取編輯腳本
-r 支持使用擴展正則表達式
-i.bak 備份文件並原處編輯
script:
'地址命令'

地址定界:
(1) 不給地址:對全文進行處理
(2) 單地址:
#: 指定的行,$:最後一行
/pattern/:被此處模式所能夠匹配到的每一行
(3) 地址範圍:
#,#
#,+#
/pat1/,/pat2/
#,/pat1/
(4) ~:步進
1~2 奇數行
2~2 偶數行

編輯命令:
d 刪除模式空間匹配的行,並立即啓用下一輪循環
p 打印當前模式空間內容,追加到默認輸出之後
a []text 在指定行後面追加文本,支持使用\n實現多行追加
i []text 在行前面插入文本
c []text 替換行爲單行或多行文本
w /path/file 保存模式匹配的行至指定文件
r /path/file 讀取指定文件的文本至模式空間中匹配到的行後
= 爲模式空間中的行打印行號
! 模式空間中匹配行取反處理

s/// 查找替換,支持使用其它分隔符,s@@@,s###
替換標記:
g 行內全局替換
p 顯示替換成功的行
w /PATH/FILE 將替換成功的行保存至文件中

高級編輯命令
P: 打印模式空間開端至\n內容,並追加到默認輸出之前
h: 把模式空間中的內容覆蓋至保持空間中
H:把模式空間中的內容追加至保持空間中
g: 從保持空間取出數據覆蓋至模式空間
G:從保持空間取出內容追加至模式空間
x: 把模式空間中的內容與保持空間中的內容進行互換
n: 讀取匹配到的行的下一行覆蓋至模式空間
N:讀取匹配到的行的下一行追加至模式空間
d: 刪除模式空間中的行
D:如果模式空間包含換行符,則刪除直到第一個換行符的模式空間中的文本,並不會讀取新的輸入行,而使用合成的模式空間重新啓動循環。如果模式空間不包含換行符,則會像發 出d命令那樣啓動正常的新循環

sed使用實例:

#sed -n '/^[^#]/p' /etc/fstab // 對#號開頭的行取反,
UUID=576ed6fe-d3e8-403d-832f-2dc117ba8d2f / xfs defaults 0 0
UUID=6e6694fc-5a2f-47a1-b96b-509621e5fc1e /boot xfs defaults 0 0
UUID=1828df2b-47f3-442d-9681-ca17539861d9 /data xfs defaults 0 0
UUID=111bb92e-4d79-486a-afaf-38ba79856d30 swap swap defaults 0 0

#seq 10 |sed -n '3,6p' //打印3到6行
3
4
5
6

#sed -n '/^b/,/^f/p' /etc/passwd //打印指定文件以字母b開頭到字母f開頭的行
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
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

#seq 10 |sed -n '1~2p' //打印奇數行
1
3
5
7
9

#seq 10 |sed '2d' // 2d 指刪除第二行
1
3
4
5
6
7
8
9
10

#seq 10 |sed '1~2d' //刪除奇數行
2
4
6
8
10

#seq 10 |sed '2~2d' //刪除偶數行
1
3
5
7
9

#sed '/^#/d' /etc/fstab |sed '/^$/d' //先刪除以#號開頭的行,再刪空行
UUID=576ed6fe-d3e8-403d-832f-2dc117ba8d2f / xfs defaults 0 0
UUID=6e6694fc-5a2f-47a1-b96b-509621e5fc1e /boot xfs defaults 0 0
UUID=1828df2b-47f3-442d-9681-ca17539861d9 /data xfs defaults 0 0
UUID=111bb92e-4d79-486a-afaf-38ba79856d30 swap swap defaults 0 0

#sed '1d;/^#/d' /etc/fstab //刪除指定行和以#號開頭的行,中間以分;隔開
UUID=576ed6fe-d3e8-403d-832f-2dc117ba8d2f / xfs defaults 0 0
UUID=6e6694fc-5a2f-47a1-b96b-509621e5fc1e /boot xfs defaults 0 0
UUID=1828df2b-47f3-442d-9681-ca17539861d9 /data xfs defaults 0 0
UUID=111bb92e-4d79-486a-afaf-38ba79856d30 swap swap defaults 0 0

#seq 10 |sed '2~2ahello' //在偶數行後插入hello, 後面插入用a
1
2
hello
3
4
hello
5
6
hello
7
8
hello
9
10
hello

#seq 10 |sed '2~2a ==='
1
2

3
4

5
6

7
8

9
10

seq 10 |sed '2~2a\ ===' //比較加轉義符的區別
1
2

3
4

5
6

7
8

9
10

#sed '/# User/awelcome to magedu' ~/.bashrc //在# User行後插入welcome to magedu

.bashrc

User specific aliases and functions

welcome to magedu

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias xp='cd /etc/sysconfig/network-scripts/'
alias tree='tree --charset ASCII'

#seq 10 |sed '2~2ihello' //在偶數行前插入hello,行前插入用i
1
hello
2
3
hello
4
5
hello
6
7
hello
8
9
hello
10

#seq 10|sed '2~2c hello' //c 把偶數行替換爲hello
1
hello
3
hello
5
hello
7
hello
9
hello

#seq 10 |sed '1~2r /etc/issue' //r /path/file 讀取指定文件的文本至模式空間中匹配到的行後
1
CentOS release 6.10 (Final)
Kernel \r on an \m

2
3
CentOS release 6.10 (Final)
Kernel \r on an \m

4
5
CentOS release 6.10 (Final)
Kernel \r on an \m

6
7
CentOS release 6.10 (Final)
Kernel \r on an \m

8
9
CentOS release 6.10 (Final)
Kernel \r on an \m


#ifconfig eth0 |sed -n '2p'
inet addr:192.168.33.135 Bcast:192.168.33.255 Mask:255.255.255.0

#ifconfig eth0 |sed -n '2!p' // ! 模式空間中匹配行取反處理
eth0 Link encap:Ethernet HWaddr 00:0C:29:71:4E:53
inet6 addr: fe80::20c:29ff:fe71:4e53/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:869 errors:0 dropped:0 overruns:0 frame:0
TX packets:550 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:81717 (79.8 KiB) TX bytes:63219 (61.7 KiB)

#sed -n 's/UUID/uuid/p' /etc/fstab // s/// 查找替換,支持使用其它分隔符,s@@@,s###
uuid=e21f9af1-9890-4abc-9a38-89be6ea69276 / ext4 defaults 1 1
uuid=aadd795d-685c-4b1b-b08f-eb0fdf3ebc8d /boot ext4 defaults 1 2
uuid=5326996d-8d2b-4c87-b819-ab3b890ed512 /data ext4 defaults 1 2
uuid=553ca22c-0b42-4256-afd3-5ef98df0fb97 swap swap defaults 0 0

備註: 替換標記
p 顯示替換成功的行
w /PATH/FILE 將替換成功的行保存至文件中

#grep root /etc/passwd |sed -r 's/root//g' // 查找替換root刪除 = 把root替換爲空
:x:0:0::/:/bin/bash
operator:x:11:0:operator:/:/sbin/nologin

#grep root /etc/passwd |sed 's/root/rooter/g' //把root替換爲rooter
rooter:x:0:0:rooter:/rooter:/bin/bash
operator:x:11:0:operator:/rooter:/sbin/nologin

#grep root /etc/passwd |sed -r 's/(root)/\1er/g' // -r 支持使用擴展正則表達式, 利用後向引用, g 行內全局替換
rooter:x:0:0:rooter:/rooter:/bin/bash
operator:x:11:0:operator:/rooter:/sbin/nologin


#sed -r "s/(^[^#])/#\1/" /etc/fstab //給不是以#號開頭的行加上#號

/etc/fstab

Created by anaconda on Sun Dec 9 22:10:28 2018

#

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

#UUID=576ed6fe-d3e8-403d-832f-2dc117ba8d2f / xfs defaults 0 0 //被操作行
#UUID=6e6694fc-5a2f-47a1-b96b-509621e5fc1e /boot xfs defaults 0 0 //被操作行
#UUID=1828df2b-47f3-442d-9681-ca17539861d9 /data xfs defaults 0 0 //被操作行
#UUID=111bb92e-4d79-486a-afaf-38ba79856d30 swap swap defaults 0 0 //被操作行


#echo "/etc/rc.d/init.d/" |sed -r 's@(.*/)([^/]+)/?@\1@' //用sed取路徑的目錄名
/etc/rc.d/

#echo "/etc/rc.d/init.d/" |sed -r 's@(.*/)([^/]+)/?@\2@' //用sed取路徑的基名
init.d

#ifconfig |sed -nr '2s/[^0-9]+([0-9.]+).*/\1/p' // 取網卡IP, CentOS 6和7 上通用
192.168.33.134


#sed -rn '/^#<VirtualHost/,/^<\/VirtualHost/ s/#//p' /etc/httpd/conf/httpd.conf //指定範圍,查找替換(把註釋去掉,替換爲空)

#name=wang
#echo abcabc |sed "s/abc/$name/g" // 查找替換,支持變量
wangwang

#seq 10 |sed -n 'n;p' //n: 讀取匹配到的行的下一行覆蓋至模式空間, P: 打印模式空間開端至\n內容,並追加到默認輸出之前
2
4
6
8
10

#seq 10 |sed '1!G;h;$!d' //G:從保持空間取出內容追加至模式空間; h: 把模式空間中的內容覆蓋至保持空間中; d: 刪除模式空間中的行
10
9
8
7
6
5
4
3
2
1


#ls /misc/cd/Packages/
authconfig-gtk-6.2.8-30.el7.x86_64.rpm
authd-1.4.3-42.el7.x86_64.rpm
autoconf213-2.13-31.el7.noarch.rpm

#ls /misc/cd/Packages/ |sed -nr 's#(.).(.).rpm#\2#p' |sort -u //統計centos安裝光盤中Package目錄下的所有rpm文件的以.分隔倒數第二個字段.
i686
noarch
x86_64

#ls /misc/cd/Packages/ |sed -nr 's#(.).(.).rpm#\2#p' |sort |uniq -c //統計centos安裝光盤中Package目錄下的所有rpm文件的以.分隔倒數第二個字段的重複次數.
2223 i686
3117 noarch
4571 x86_64

===============================================================================

用正則表達式匹配手機號碼

#grep -oE "1[0-9]{10}" tel.txt

用正則表達式匹配email郵箱

#egrep -o "([[:alnum:]]|_)+@[[:alnum:]]+.com$" mail.txt

使用正則正則取IP

#ifconfig |grep -Eo "([0-9]{1,3}.){3}[0-9]{1,3}"

=================================================================================

yum源配置

[root@centos6 yum.repos.d]#cat epel.repo //epel源
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/6/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-6Server
enabled=1

[root@centos6 yum.repos.d]#cat aliyun.repo //aliyun源
[aliyun]
name=aliyun
baseurl=https://mirrors.aliyun.com/centos/7.5.1804/os/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/7.5.1804/os/x86_64/RPM-GPG-KEY-CentOS-7
enabled=1

[root@centos6 yum.repos.d]#cat nginx.repo //nginx源
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1

[root@centos6 yum.repos.d]#cat cdrom.repo //本地源
[cdrom]
name=cdrom repo
baseurl=file:///misc/cd
gpgkey=file:///misc/cd/RPM-GPG-KEY-CentOS-6
gpgcheck=1
enabled=1

============================================================================================

Centos7.5 實現光盤自動掛載

[root@centos7 ~]#rpm -q autofs //查看autofs包是否安裝
package autofs is not installed

[root@centos7 ~]#yum -y install autofs //yum安裝autofs包

[root@centos7 ~]#systemctl enable autofs; systemctl start autofs

[root@centos7 ~]#cd /misc/cd //只要訪問/misc/cd這個目錄,光盤就會自動掛載

[root@centos7 cd]#ls
CentOS_BuildTag EULA images LiveOS repodata RPM-GPG-KEY-CentOS-Testing-7
EFI GPL isolinux Packages RPM-GPG-KEY-CentOS-7 TRANS.TBL

[root@centos7 cd]#df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 52403200 5935708 46467492 12% /
devtmpfs 483096 0 483096 0% /dev
tmpfs 498988 0 498988 0% /dev/shm
tmpfs 498988 14948 484040 3% /run
tmpfs 498988 0 498988 0% /sys/fs/cgroup
/dev/sda3 31441920 33388 31408532 1% /data
/dev/sda1 1038336 159296 879040 16% /boot
tmpfs 99800 40 99760 1% /run/user/0
/dev/sr0 9176232 9176232 0 100% /misc/cd //驗證已經掛載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章