*sed可以实现一些grep的功能,但是稍微麻烦了一些,sed的强项在于删除文件中的内容和替换
sed实现grep检索功能:
1.根据关键字检索:
[[email protected] ~]# sed -n ‘/root/‘p passwd.txt
*使用sed检索时需要在关键字前加-n参数,关键字后加p,关键词还需要用 // 包括起来
2.关键字有特殊符号时加上 -r 参数或使用脱义符
[[email protected] ~]# sed -nr ‘/o+t/‘p passwd.txt
3.打印指定行:(打印指定行时直接写行号即可,不需要加//符号括起来)
[[email protected] ~]# sed -n ‘5‘p passwd.txt
[[email protected] ~]# sed -n ‘5,$‘p passwd.txt #打印第五行至最后一行
4.-e 参数:使用多个表达式:
[[email protected] ~]# sed -e ‘1‘p -e ‘/root/‘p -n passwd.txt
*打印第一行内容并检索包含root的行,如果第一行同时包含root,那么第一行会被打印两次
5.不区分大小写:(加上大写的i)
[[email protected] ~]# sed -n ‘/testword/‘Ip passwd.txt
sed删除功能:
1.删除打印结果中指定的行:
[[email protected] ~]# wc -l passwd.txt
22 passwd.txt
#查看文件行数
[[email protected] ~]# sed ‘1,20‘d passwd.txt
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
linux01:x:1000:1000::/home/linux01:/bin/bash
#删除第1-20行,打印剩余的行
[[email protected] ~]# wc -l passwd.txt
22 passwd.txt
#该操作不会真实删除文件内容,只是删除打印结果
2.-i 参数:删除文件中指定的行(加上 -i参数后会真实删除文件内容)
[[email protected] ~]# wc -l passwd.txt
22 passwd.txt
[[email protected] ~]# sed -i ‘1,20‘d passwd.txt
[[email protected] ~]# wc -l passwd.txt
2 passwd.txt
*用于在删除大日志文件内容时比较常用
sed替换功能:
1.根据关键字替换:
[[email protected] ~]# cat passwd.txt
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# sed ‘s/chrony/sed_test/g‘ passwd.txt
sed_test:x:998:996::/var/lib/sed_test:/sbin/nologin
linux01:x:1000:1000::/home/linux01:/bin/bash
*格式: ‘s/ 被替换关键字 / 替换内容 /g‘
2.特殊符号匹配时加 -r 参数:
[[email protected] ~]# cat passwd.txt
nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# sed -r ‘s/n+y/sed_test/g‘ passwd.txt
sed_test:x:998:996::/var/lib/chronwy:/sbin/nologin
linux01:x:1000:1000::/home/linux01:/bin/bash
3.分段替换位置:
[[email protected] ~]# cat passwd.txt
nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# sed -r ‘s/([^:]+):(.*):([^:]+)/\3:\2:\1/g‘ passwd.txt
/sbin/nologin:x:998:996::/var/lib/chronwy:nnnnny
/bin/bash:x:1000:1000::/home/linux01:linux01
*使用冒号分割为3段,将第三段和第一段位置互换
4.配置关键词为目录路径时加上脱义符或使用其他替换符号:
[[email protected] ~]# cat passwd.txt
nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# sed ‘s/\/bin\/bash/AAAAAAA/g‘ passwd.txt
nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
linux01:x:1000:1000::/home/linux01:AAAAAAA
[[email protected] ~]# sed ‘s#/bin/bash#AAAAAAA#g‘ passwd.txt
nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
linux01:x:1000:1000::/home/linux01:AAAAAAA
5.删除所有字母:
[[email protected] ~]# cat passwd.txt
nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# sed ‘s/[a-zA-Z]//g‘ passwd.txt
::998:996::///://
01::1000:1000:://01://
6.在每行前添加内容:
[[email protected] ~]# cat passwd.txt
nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# sed -r ‘s/.*/sed_test:&/g‘ passwd.txt
sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
*匹配每行所有内容:.*,&符号代表.*的内容
7.真实替换文件中的内容:-i 参数(以上6条示例都不会实际更改文件内容,只更改替换完成的打印输出结果)
[[email protected] ~]# cat passwd.txt
nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# sed -i ‘s/.*/sed_test:&/g‘ passwd.txt
[[email protected] ~]# cat passwd.txt
sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
补充:
把每个单词的第一个小写字母变大写:
sed ‘s/\b[a-z]/\u&/g‘ filename
把所有小写变大写:
sed ‘s/[a-z]/\u&/g‘ filename
大写变小写:
sed ‘s/[A-Z]/\l&/g‘ filename
sed在某一行最后添加一个数字
sed -r ‘s/(^a.*)/\1 12/‘ test
sed -r ‘s/^a.*/& 12/‘ test
打印1到100行含某个字符串的行
sed -n ‘1,100{/abc/p}‘ 1.txt
*awk相比grep/egrep/sed的功能更加强大,默认支持拓展正则表达式,grep需要加 -E 参数,sed需要加 -r 参数
1.分割文件内容打印指定段数:
[[email protected] ~]# cat test.txt
zhangsan 100
lisi 92
wangwu 95
user1 88
user2 93
[[email protected] ~]# awk ‘{print $1}‘ test.txt
zhangsan
lisi
wangwu
user1
user2
*默认以空格为分隔符,$指定打印的段数
2.-F参数:指定分隔符:
[[email protected] ~]# cat passwd.txt
AAAA:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk -F ‘:‘ ‘{print $3}‘ passwd.txt
nnnnny
linux01
#打印多段时用逗号区分
[[email protected] ~]# awk -F ‘:‘ ‘{print $1,$3}‘ passwd.txt
AAAA nnnnny
AAAA linux01
#指定打印结果中段与段的分割符号
[[email protected] ~]# awk -F ‘:‘ ‘{print $1"-->"$3}‘ passwd.txt
AAAA-->nnnnny
AAAA-->linux01
*awk {print $0}表示打印所有,相当于cat
3.awk检索:(相当于grep)
[[email protected] ~]# cat passwd.txt
AAAA:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk ‘/nnn/‘ passwd.txt
AAAA:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
4.检索指定段中包含关键字的行:
[[email protected] ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk -F ‘:‘ ‘$1 ~/AAA/‘ passwd.txt
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
5.多个表达式组合使用:
[r[email protected] ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk -F ‘:‘ ‘/linux/ {print $1,$3} /nnn/ {print $2,$3}‘ passwd.txt
sed_test nnnnny
AAAA linux01
*打印包含linux关键字的行的第1和第3段,打印包含nnn关键字的第2和第3段
6.检索多个关键词的行并指定打印段:
[[email protected] ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk -F ‘:‘ ‘/linux|nnn/ {print $1}‘ passwd.txt
ABCD
AAAA
7.通过运算符号检索:
[[email protected] ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk -F ‘:‘ ‘$5==1000‘ passwd.txt
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk -F ‘:‘ ‘$5==1000 {print $1}‘ passwd.txt
AAAA
8.针对数字判断时,不加双引号,否则判断条件会被当做字符串而不是数字(数字当做字符串判断时,根据ASCII码判断,998大于1000):
[[email protected] ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk -F ‘:‘ ‘$5<1000‘ passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
#加上双引号后1000被当做了字符串,而不是数字
[[email protected] ~]# awk -F ‘:‘ ‘$5<"1000"‘ passwd.txt
[[email protected] ~]#
9.字符串判断:(判断字符串时判断条件加上双引号)
[[email protected] ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk -F ‘:‘ ‘$9!="/sbin/nologin"‘ passwd.txt
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
10.两个字段之间比较和多个条件检索:
[[email protected] ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk -F ‘:‘ ‘$5>$6‘ passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
[[email protected] ~]# awk -F ‘:‘ ‘$5>$6 {print $5,$6}‘ passwd.txt
998 996
[[email protected] ~]# awk -F ‘:‘ ‘$5>999‘ passwd.txt
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk -F ‘:‘ ‘$5>900 && $6<999‘ passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
[[email protected] ~]# awk -F ‘:‘ ‘$5>999 || $9=="/sbin/nologin"‘ passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
*最后一种字符串判断除了用==符号还可以用~检索
11.OFS:指定打印结果分隔符:
[[email protected] ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk -F ‘:‘ ‘{OFS="***"} {print $1,$2,$3}‘ passwd.txt
ABCD***sed_test***nnnnny
AAAA***sed_test***linux01
[[email protected] ~]# awk -F ‘:‘ ‘{OFS="***"} $5<1000 {print $1,$2,$3}‘ passwd.txt
ABCD***sed_test***nnnnny
12.NR:在每行前面显示行数:
[[email protected] ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk -F ‘:‘ ‘{print NR":" $0}‘ passwd.txt
1:ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
2:AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
13.NF:在每行前面显示段数:
[[email protected] ~]# awk -F ‘:‘ ‘{print NF":" $0}‘ passwd.txt
9:ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
9:AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
14.根据行数或者段数显示指定的行:
1)显示行号小于2的行:
[[email protected] ~]# awk -F ‘:‘ ‘{print NR":" $0}‘ passwd.txt
1:ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
2:AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk ‘NR<2‘ passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
2)显示前10行并且第一段为AAAA的行:
[[email protected] ~]# awk -F ‘:‘ ‘NR<10 && $1 ~/AAAA/‘ passwd.txt
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
15.=和==的区别:(==是匹配相等的数字或字符串,=是赋值)
[[email protected] ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk -F ":" ‘$1=="ABCD"‘ passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
[[email protected] ~]# awk -F ":" ‘$1="hello"‘ passwd.txt
hello sed_test nnnnny x 998 996 /var/lib/chronwy /sbin/nologin
hello sed_test linux01 x 1000 1000 /home/linux01 /bin/bash
16.求和:
[[email protected] ~]# cat passwd.txt
ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
[[email protected] ~]# awk -F ‘:‘ ‘{(tot=tot+$5)}; END {print tot}‘ passwd.txt
1998
补充:
. 表示任意1个字符
* a b o 前面的这个字符有0或者多个
abc* ===> ab、abccc
.* 贪婪匹配
? 扩展正则 表示:?前面的字符有0个或1个
a1? ==> a or a1
+ 扩展正则 表示:+前面的字符有1个或多个
| 扩展正则 表示:或者
egrep ‘abc|123‘ 1.txt
[ ] 表示方括号里面的1个字符
[a-zA-Z0-9] 表示所有大小写字母和数字
[abc] 表示a或b或c
[a|@] 表示a或|或@
[^] 表示非,取反
^ 表示开头
$ 表示结尾
{ } 表示范围
a{1,5} ===> a或aa或aaa或aaaa或aaaaa
b{3} ===> bbb
( ) 小括号里面的字符看成一个整体
(abc){2} ==》 abcabc
(abc)+ ==>abc或abcabc或abc*n
abc{2} ==> abcc
扩展正则符号: ? + | { } 使用时grep -E 、sed -r
原文地址:https://blog.51cto.com/14520558/2439488