文本三剑客之sed命令--文本替换;grep命令---文本过滤
目錄
1.sed簡介
2.sed的p命令--打印匹配行
3.sed的d命令--刪除指定行
4.sed的a命令--在匹配行后面追加
5.sed的i命令--在匹配行前面插入
6.sed的c命令--整行替換
7.sed的r命令--將文件的內容讀入
8.sed的w命令--將文本寫入文件
9.sed的s命令--字符串替換(匹配正則表達式)核心用法
一.sed命令
1.sed簡介
sed是一種支持正則表達式的非交互式流編輯器,是腳本中修改文本或者文本替換的最佳工具
sed本身也是一個管道命令,它可以將數據進行替換、刪除、新增、選取特定行等功能
# 語法格式
sed ? [選項] ?sed編輯命令 ?輸入文件
# 常用選項
-n:只顯示匹配處理的行(否則會輸出所有)
-r:支持擴展正則表達式
-i:直接在文件中進行修改,而不是輸出到屏幕
-e:執(zhí)行多個編輯命令時(一般用;代替)
-f:從腳本文件中讀取內容并執(zhí)行(文件中的編輯命令每行一個,不用;隔開)
# 常用編輯命令
p:打印匹配行?? ??? ? ?print
d:刪除指定行?? ??? ? ?delete
a:在匹配行后面追加?? ?append
i:在匹配行前面插入 ?insert
c:整行替換
r:將文件的內容讀入 ?read
w:將文本寫入文件?? ?write
s:字符串替換(匹配正則表達式)substitution
=: 輸出行號
###以上的操作都既可以根據行號,又可以根據模式匹配
2.sed的p命令--打印匹配行
###打印第1行 [root@xieshan ~]# sed -n '1p' /etc/passwd root:x:0:0:root:/root:/bin/bash###打印第2行到第4行 [root@xieshan ~]# sed -n '2,4p' /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 [root@xieshan ~]# sed -n '2,+2p' /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###打印最后一行 [root@xieshan ~]# sed -n '$p' /etc/passwd cali123:x:1026:1028::/home/cali123:/bin/bash###打印第一行和最后一行 [root@xieshan ~]# sed -n '1p;$p' /etc/passwd root:x:0:0:root:/root:/bin/bash cali123:x:1026:1028::/home/cali123:/bin/bash###不打印第4行到最后一行,即打印第1行到第3行,并且顯示行號 [root@xieshan ~]# sed -n '4,$!{=;p}' /etc/passwd 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin###打印匹配的adm行到匹配的sync行 [root@xieshan ~]# sed -n '/adm/,/sync/p' /etc/passwd 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###打印第1行,第3行,第5行 [root@xieshan ~]# sed -n '1p;3p;5p' /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:2:2:daemon:/sbin:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin###打印第3行和第5行 [root@xieshan ~]# sed -n -e '3p' -e '5p' /etc/passwd daemon:x:2:2:daemon:/sbin:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin### 從第一行開始,隔兩行輸出一行 [root@xieshan 0713]# cat /etc/passwd -n|sed -n '1~2p' #2是步長值,隔兩個輸出1 root:x:0:0:root:/root:/bin/bash3 daemon:x:2:2:daemon:/sbin:/sbin/nologin5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin11 games:x:12:100:games:/usr/games:/sbin/nologin13 nobody:x:99:99:Nobody:/:/sbin/nologin###匹配以s開頭的行,根據正則模式來匹配,沒有根據行號 [root@xieshan 0713]# sed -n '/^s/p' /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin sc:x:1000:1000:sc:/home/sc:/bin/bash sc1:x:1001:1001::/home/sc1:/bin/bash###不顯示/etc/ssh/sshd_config里面的無效行,也就是不顯示空行或者以#開頭的行 [root@xieshan 0713]# sed -rn '/^#|^$/!p' /etc/ssh/sshd_config #|是擴展正則 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key SyslogFacility AUTHPRIV AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication yes ChallengeResponseAuthentication no GSSAPIAuthentication yes GSSAPICleanupCredentials no UsePAM yes X11Forwarding yes AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS Subsystem sftp /usr/libexec/openssh/sftp-server###顯示以/符號結尾的行,需要使用轉義符號 [root@xieshan 0713]# df -h|sed -n '/\/$/p' /dev/mapper/centos-root 17G 3.9G 14G 23% / [root@xieshan 0713]# df -h|egrep '/$' #通常使用egrep查找 /dev/mapper/centos-root 17G 3.9G 14G 23% /shell里的變量傳遞給sed--》傳參問題--〉使用雙引號,然后變量外面加上花括號
[root@xieshan 0713]# a=5 [root@xieshan 0713]# b=8 [root@xieshan 0713]# sed -n "${a}p;${b}p" /etc/passwd lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin halt:x:7:0:halt:/sbin:/sbin/halt [root@xieshan 0713]# [root@xieshan 0713]# sg=sc [root@xieshan 0713]# sed -n "/${sg}/p" /etc/passwd sc:x:1000:1000:sc:/home/sc:/bin/bash3.sed的d命令--刪除指定行
[root@xieshan lianxi]# cat cali.txt cali hunan yongz rose jack###以下的操作都只是在屏幕上顯示,實際文件并沒有修改,要想直接在文件中進行修改,需要加上-i選項 #刪除2到4行 [root@xieshan lianxi]# sed '2,4d' cali.txt cali jack#刪除含有字符串z的行 [root@xieshan lianxi]# sed '/'z'/d' cali.txt cali hunan rose jack#刪除不含有字符串z的行 [root@xieshan lianxi]# sed '/'z'/!d' cali.txt yongz #刪除空行和注釋 [root@xieshan lianxi]# sed -r '/^$|^#/d' cali.txt cali hunan yongz rose jack#刪除空行以及以#開頭的行 [root@xieshan 0713]# sed -i -r '/^#|^$/d' sshd_config [root@xieshan 0713]# cat sshd_config HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key SyslogFacility AUTHPRIV AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication yes ChallengeResponseAuthentication no GSSAPIAuthentication yes GSSAPICleanupCredentials no UsePAM yes X11Forwarding yes AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE AcceptEnv XMODIFIERS Subsystem sftp /usr/libexec/openssh/sftp-server [root@xieshan 0713]#4.sed的a命令--在匹配行后面追加
###追加操作可以根據行號和模式匹配進行操作 [root@xieshan lianxi]# cat cali.txt cali hunan yongz rose jack#在第2行后面追加字符串1000 [root@xieshan lianxi]# sed '2a 1000' cali.txt cali hunan 1000 yongz rose jack#在包含root的行下面追加字符串1000 [root@xieshan lianxi]# sed '/root/a 1000' /etc/passwd root:x:0:0:root:/root:/bin/bash 1000 operator:x:11:0:operator:/root:/sbin/nologin 10005.sed的i命令--在匹配行前面插入
### 插入操作可以根據行號和模式匹配進行操作 [root@xieshan lianxi]# cat cali.txt cali hunan yongz rose jack#在cali.txt的最后一行前面插入字符串1000 [root@xieshan lianxi]# sed '$i 1000' cali.txt cali hunan yongz rose 1000 jack#在包含root的行的前面插入字符串1000 [root@xieshan lianxi]# sed '/root/i 1000' /etc/passwd 1000 root:x:0:0:root:/root:/bin/bash 1000 operator:x:11:0:operator:/root:/sbin/nologin6.sed的c命令--整行替換
#### 更改整行操作可以根據行號和模式匹配進行操作[root@xieshan lianxi]# cat cali.txt cali hunan yongz rose jack#將第3行整行替換成字符串1000 [root@xieshan lianxi]# sed '3c 1000' cali.txt cali hunan 1000 rose jack#將root所在的行 整行替換成字符串1000 [root@xieshan lianxi]# sed '/root/c suda' /etc/passwd suda bin:x:1:1:bin:/bin:/sbin/nologin[root@xieshan 0713]# cat ifcfg-ens33 BOOTPROTO="none" NAME="ens33" DEVICE="ens33" ONBOOT="yes" IPADDR=192.168.2.43 PREFIX=24 GATEWAY=192.168.2.1 DNS1=114.114.114.114 [root@xieshan 0713]# sed -i '4c ONBOOT="no"' ifcfg-ens33 [root@xieshan 0713]# cat ifcfg-ens33 BOOTPROTO="none" NAME="ens33" DEVICE="ens33" ONBOOT="no" IPADDR=192.168.2.43 PREFIX=24 GATEWAY=192.168.2.1 DNS1=114.114.114.114 [root@xieshan 0713]# sed -i '/^BOOTPROTO/c BOOTPOROTO="yes"' ifcfg-ens33 [root@xieshan 0713]# cat ifcfg-ens33 BOOTPOROTO="yes" NAME="ens33" DEVICE="ens33" ONBOOT="no" IPADDR=192.168.2.43 PREFIX=24 GATEWAY=192.168.2.1 DNS1=114.114.114.114 [root@xieshan 0713]# sed -i '/^ONBOOT/ s/no/yes/' ifcfg-ens33 [root@xieshan 0713]# cat ifcfg-ens33 BOOTPOROTO="yes" NAME="ens33" DEVICE="ens33" ONBOOT="yes" IPADDR=192.168.2.43 PREFIX=24 GATEWAY=192.168.2.1 DNS1=114.114.114.1147.sed的r命令--將文件的內容讀入
### 讀入操作可以根據行號和模式匹配進行操作# 在/etc/passwd文件的末尾后面讀入cali.txt文件的內容 [root@xieshan lianxi]# sed '$r cali.txt' /etc/passwd califeng:x:1025:1027::/home/califeng:/bin/bash cali123:x:1026:1028::/home/cali123:/bin/bash cali hunan yongz rose jack8.sed的w命令--將文本寫入文件
[root@scchen1 /]# cat selinux# This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted#把selinux中以#開頭的行,不寫入a.txt文件中 [root@scchen1 /]# sed '/^#/!w a.txt' selinux [root@scchen1 /]# cat a.txtSELINUX=disabled SELINUXTYPE=targeted9.sed的s命令--字符串替換(匹配正則表達式)核心用法
? ? ? ? 1.sed替換的基本語法:
?sed?'s/原字符串/替換的字符串'? ? ? ? 2.特殊的字符樣式需要使用轉義符號\,但是單引號內無法使用轉義字符,所以sed替換的語法還可以寫成:
?sed?"s/原字符串/替換的字符串"? ? ? ? 3.可以在匹配公式最后面加一個g,這樣就會替換每一個匹配的字符串,否則只替換每行的第一個匹配的字符串
?sed?"s/原字符串/替換的字符串/g"? ? ? ? 4.一些特殊字符的使用:
# ^表示以什么開頭,$表示以什么結尾 # 注意這里的`&`符號,如果沒有`&`,就會直接將匹配到的字符串替換掉 sed 's/^/添加的頭部&/g' #在所有行首添加 sed 's/$/&添加的尾部/g' #在所有行末添加 sed '2s/原字符串/替換字符串/g' #替換第2行 sed '$s/原字符串/替換字符串/g' #替換最后一行 sed '2,5s/原字符串/替換字符串/g' #替換2到5行 sed '2,$s/原字符串/替換字符串/g' #替換2到最后一行? ? ?
[root@xieshan 0713]# cat /etc/selinux/config# This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=enforcing # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted [root@xieshan 0713]# sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config [root@xieshan 0713]# cat /etc/selinux/config# This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted? 5.多個替換可以在同一條命令中執(zhí)行,使用;分隔,或者使用-e選項
# 同時執(zhí)行兩個替換規(guī)則 sed 's/^/添加的頭部&/g;s/$/&添加的尾部/g' sed 's/^/添加的頭部&/g -e s/$/&添加的尾部/g'6.編輯一個test.txt文件,內容如下:
0.0.0.0
1.1.1.1
2.2.2.2
輸出如下形式:
0.0.0.0:80,1.1.1.1:80,2.2.2.2:80
[root@localhost shell]#sed -i 'N;N;s/\n/:80,/g;s/$/:80/' test.txt [root@localhost shell]# cat test.txt 0.0.0.0:80,1.1.1.1:80,2.2.2.2============= #xargs:將多行變成單行。將前面的標準輸出通過管道進行標準輸入得到的內容分割成一個一個的參數,使用空格隔開。 [root@localhost shell]#cat test.txt.bak|xargs 0.0.0.0 1.1.1.1 2.2.2.2 [root@localhost shell]#cat test.txt.bak|xargs|awk '{print $1":80,"$2":80,",$3":80"}' 0.0.0.0:80,1.1.1.1:80, 2.2.2.2:807.sed命令中&的用法
&表示替換命令中的匹配模式:代表模式匹配到的內容,然后可以引用 [root@localhost shell]# echo "i have a fat cat" i have a fat cat [root@localhost shell]# echo "i have a fat cat"|sed 's/.at/".at"/g' i have a ".at" ".at" [root@localhost shell]# echo "i have a fat cat"|sed 's/.at/"&"/g' i have a "fat" "cat"###將所有三位的數字后面加上0,四位數的不加,注意使用詞邊界 [root@localhost shell]# cat renxiaojing.txt ........ tanxue zhang shuaishuai 123 zhang 456pengpeng pengpeng pengpeng pengpeng xuyalan jingjing jingjing jingjing [root@localhost shell]# sed -i -r 's/\<[0-9]{3}\>/&0/g' renxiaojing.txt [root@localhost shell]# cat renxiaojing.txt ..... tanxue zhang shuaishuai 1230 zhang4560pengpeng pengpeng pengpeng pengpeng xuyalan jingjing jingjing jingjing#給文件中所有的大寫字母后面加上2022 [root@localhost shell]# sed -n -r 's/[A-Z]/&2022/gp' test1.txt abcH2022H2022H2022H2022dddde abF2022K2022D2022dddde isO2022O2022O2022ishabxil H2022E2022P2022R2022E2022doodcm8.\ ?\:轉義字符
[root@localhost shell]# sed -n '/13\/Jul\/2022:16:53:58/,/13\/Jul\/2022:16:56:21/p' access.log sed -n [root@localhost shell]# sed -n '#13/Jul\/2022:16:53:58#,#13/Jul/2022:16:56:21#p' access.loglocalhost shell]# sed -n 's#Jul#Aug#gp' access.log|head -5 修改時間:date -s "修改的時間"9.標簽
####小圓括號里面匹配的就是一個標簽,后面再替換的時候可以引用,第一個小圓括號里的是\1,以此類推。 [root@localhost shell]# echo aaa bbb ccc |sed -r 's/([a-z]+) ([a-z]+) ([a-z]+)/\3 \2 \1/' ccc bbb aaa [root@localhost shell]# echo aaa bbb ccc |sed -r 's/([a-z]+) ([a-z]+) ([a-z]+)/\3/' ccc [root@localhost shell]# echo aaa bbb ccc |awk '{print $3,$2,$1}' ccc bbb aaa10.將以下文本倒序輸出
0.0.0.0
1.1.1.1
2.2.2.2
[root@localhost shell]# tac test.txt.bak 2.2.2.2 1.1.1.1 0.0.0.0 方法二: [root@localhost shell]# cat test.txt.bak|xargs|awk '{print $3"\n"$2"\n"$1}' 2.2.2.2 1.1.1.1 0.0.0.0二.grep命令
1.grep的常用兩種格式:
第一種:
cat /etc/password | grep "root"
第二種:
grep "root" /etc/passwd
grep命令還可以疊加使用
# 將/etc/passwd里沒有出現root和nobody的行取出來
grep -v root /etc/passwd | grep -v nobody
2.常用的grep選項:
-i?? ?ignore?? ?# 不區(qū)分大小寫
-v?? ?invert?? ?# 取反,只打印沒有匹配的,而匹配的反而不打印(只針對整行取反)
-n?? ?number?? ?# 顯示匹配行及行號
-c?? ?count?? ?# 輸出匹配行的數目
-o?? ?only-matching
# 只顯示匹配行中匹配正則表達式的那部分
# 經常和 wc -l 命令搭配使用
-r?? ?recursive?? ?# 遞歸的對目錄下的所有文件(包括子目錄)進行grep查找
-E?? ?Extend?? ?# 開啟擴展的正則表達式
egrep = grep -E
-A n?? ?after?? ?# 顯示匹配到的字符串所在的行及其后n行
-B n?? ?before?? ?# 顯示匹配到的字符串所在的行及其前n行
-C n?? ?context?? ?# 顯示匹配到的字符串所在的行及其前后各n行
3.匹配字符
[]?? ??? ?# 指定范圍內的單個字符
[0-9]?? ?# 匹配0到9中的任意一個數字
[0-Z]?? ?# 匹配所有的字母和數字
[a-z]?? ?# 匹配a~z的所有的小寫字母中的任意一個
[A-Z]?? ?# 匹配A~Z的所有的大寫字母中的任意一個
[a-Z]?? ?# 匹配所有的大小寫字母中的任意一個
[abc]?? ?# 匹配一個字符,這個字符必須是abc中的一個
[^]?? ??? ?# 指定范圍外的的單個字符,取反
[^123]?? ?# 匹配一個字符,這個字符是除了1、2、3以外的所有字符
.?? ??? ?# 代表單個任意字符
.*?? ??? ?# 代表任意個任意字符
4.匹配次數
??? ??? ?# 前面的字符出現0次或1次
+?? ??? ?# 前面的字符出現1次以上
*?? ??? ?# 前面的字符出現任意多次
? 、* 、+ 默認只能修飾前面一個字符
{n}?? ??? ?# 表示前面的字符出現n次
{n,m}?? ?# 表示前面的字符最少出現n次,最多出現m次
{n, }?? ?# 表示前面的字符最少出現n次
5.位置錨定
^?? ??? ?# 錨定行的開始
'^suda'匹配所有以suda開頭的行
$?? ??? ?# 錨定行的結束
'suda$'匹配所有以suda結尾的行
^$?? ??? ?# 匹配空白行
\b?? ??? ?# 單詞鎖定符
\b或\<?? ?# 錨定單詞的詞首
\b或\>?? ?# 錨定單詞的詞尾
# 舉例
'\bsuda\b'只匹配suda
'\bsuda'只匹配單詞以suda開頭
'suda\b'只匹配單詞以suda結尾
6.分組以及引用
()?? ??? ?# 利用圓括號可以實現多個字符分組
?? ??? ?# 在圓括號中利用"|"實現或者的功能
?? ?
# 舉例
(aa|bb){2}
總結
以上是生活随笔為你收集整理的文本三剑客之sed命令--文本替换;grep命令---文本过滤的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTC推出了VIVE Comos 全新
- 下一篇: Openpilot EP1:Openpi