grep----过滤、筛选
grep “字符串”
grep -v “字符串”
grep ^“字符串” 以…开头
grep “字符串”$
grep ^$ 空行
grep -i 不区分大小写
grep -o 只显示匹配的部分
sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”,接着用
sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容发送往屏幕。然后读入下行,执行下一个循环。
如果没有诸如“D”的特殊命令,那会在两个循环之间清空模式空间,但不能清除保留空间。这样不断重复,直到文件末尾。
文件内容并没有改变,除非你使用重定向存储输出或-i。
选项
-n 不打印模式空间
-e 执行脚本、表达式来处理
-f 执行动作从文件读取执行
-i 修改原文件
-r 使用扩展正则表达式
命令
s/regexp/replace/ 替换字符串
p 打印当前模式空间
P(大写) 打印模式空间的第一行
d 删除模式空间,开始下一个循环
D 删除模式空间的第一行,开始下一个循环
= 打印当前行号
a \text 当前行追加文本
i \text 当前行上面插入文本
c \text 所选行替换新文本
q 立即退出sed脚本
r 追加文本来自文件
h H 复制/追加模式空间到保持空间
g G 复制/追加保持空间到模式空间
x 交换模式空间和保持空间内容
l 打印模式空间的行,并显示控制字符$
n N 读取/追加下一行输入到模式空间
w filename 写入到当前模式空间到文件
! 取反、否定
& 引用已匹配字符串
地址
first~step 从first开始,每step步输出值
$ 匹配最后一行
/regexp/ 正则表达式匹配行
number 只匹配指定行
addr1,addr2 开始匹配addr1行开始,直接addr2行结束
addr1,+N 从addr1行开始,向后的N行
addr1,~N 从addr1行开始,到N行结束
[root@rhel8 ~]# cat /tmp/services
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
匹配打印(p)
(1)打印匹配blp5开头的行
[root@rhel8 ~]#
[root@rhel8 ~]# cat /tmp/services | sed -n '/^blp5/p'
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
(2)打印第一行
[root@rhel8 ~]# cat /tmp/services | sed -n '1p'
nimgtw 48003/udp # Nimbus Gateway
(3)打印第一行至第三行
[root@rhel8 ~]# cat /tmp/services | sed -n '1,3p'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
(4)打印奇数行
[root@rhel8 ~]# seq 10 | sed -n '1~2p'
1
3
5
7
9
(5)打印匹配行及后一行
[root@rhel8 ~]# cat /tmp/services | sed -n '/blp5/,+1p'
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
(6)打印最后一行
[root@rhel8 ~]# cat /tmp/services | sed -n '$p'
iqobject 48619/udp # iqobject
(7)不打印最后一行
[root@rhel8 ~]# cat /tmp/services | sed -n '$!p'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
(8)匹配范围
[root@rhel8 ~]# cat /tmp/services | sed -n '/^blp5/,/^com/p'
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
匹配blp5所在行到最后一行
[root@rhel8 ~]# cat /tmp/services | sed -n '/blp5/,$p'
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(9)引用系统变量,用引号
[root@rhel8 ~]# cat /tmp/services |sed -n ''$a',3p'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
或者
[root@rhel8 ~]# cat /tmp/services |sed -n "$a,3p"
[root@rhel8 ~]# cat /tmp/services | sed '/blp5/d'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
[root@rhel8 ~]# cat /tmp/services | sed '1d'
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
[root@rhel8 ~]# cat /tmp/services | sed '1~2d' 删除奇数行
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/udp # iqobject
[root@rhel8 ~]# cat /tmp/services | sed '1,3d'
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
打印时把匹配的打印出来,删除时把匹配的删除,删除只是不用-n选项
练习:去除空格/etc/httpd/conf/httpd.conf文件的空行或开头#号的行
sed '/^#/d;/^$/d' /etc/httpd/conf/httpd.conf
(1)替换blp5字符串为test
[root@rhel8 ~]# cat /tmp/services | sed 's/blp5/test/'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
test 48129/tcp # Bloomberg locator
test 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
全局替换加g
[root@rhel8 ~]# cat /tmp/services | sed 's/blp5/test/g'
(2)替换开头是blp5的字符串并打印
[root@rhel8 ~]# cat /tmp/services | sed -n 's/^blp5/test/p'
test 48129/tcp # Bloomberg locator
test 48129/udp # Bloomberg locator
(3)使用&命令引用匹配内容并替换
[root@rhel8 ~]# cat /tmp/services | sed 's/48049/&.123/'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049.123/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
ip加单引号
[root@rhel8 ~]# echo '10.10.10.1 10.10.10.2 10.10.10.3' | sed -r 's/[^ ]+/"&"/g'
"10.10.10.1" "10.10.10.2" "10.10.10.3"
(4)对1-5行的blp5进行替换
[root@rhel8 ~]# cat /tmp/services | sed '1,5s/blp5/test/'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
test 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(5)对匹配行进行替换,先匹配出来48129/tcp所在行,然后再将改行的blp5字符串替换为test
[root@rhel8 ~]# cat /tmp/services | sed '/48129\/tcp/s/blp5/test/'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
test 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(6)二次匹配替换
[root@rhel8 ~]# cat /tmp/services | sed 's/blp5/test/;s/3g/4g/'
nimgtw 48003/udp # Nimbus Gateway
4gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
test 48129/tcp # Bloomberg locator
test 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(7)分组使用
[root@rhel8 ~]# cat /tmp/services | sed -r 's/(.*)(480.*)(#.*)/\1\2test \3/'
nimgtw 48003/udp test # Nimbus Gateway
3gpp-cbsp 48049/tcp test # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(8)将协议与端口号位置互换
[root@rhel8 ~]# cat /tmp/services | sed -r 's/(.*)(\<[0-9]+\>)\/(tcp|udp)(.*)/\1\3\/\2\4/'
nimgtw udp/48003 # Nimbus Gateway
3gpp-cbsp tcp/48049 # 3GPP Cell Broadcast Service Protocol
isnetserv tcp/48128 # Image Systems Network Services
isnetserv udp/48128 # Image Systems Network Services
blp5 tcp/48129 # Bloomberg locator
blp5 udp/48129 # Bloomberg locator
com-bardac-dw tcp/48556 # com-bardac-dw
com-bardac-dw udp/48556 # com-bardac-dw
iqobject tcp/48619 # iqobject
iqobject udp/48619 # iqobject
(9)位置调换
[root@rhel8 ~]# echo "abc cde xyz" | sed -r 's/(.*)x/\1X/'
abc cde Xyz
[root@rhel8 ~]# echo "abc cde xxyxz" | sed -r 's/(.*)x/\1X/'
abc cde xxyXz
[root@rhel8 ~]# echo "abc cde xxyxz" | sed -r 's/(.*)y/\1Y/'
abc cde xxYxz
将cde和456互换位置
[root@rhel8 ~]# echo "abc:cde;123:456" | sed -r 's/([^:]*)(;.*:)([^:]+$)/\3\2\1/'
abc:456;123:cde
(10)注释匹配行后的多少行
[root@rhel8 ~]# seq 10 |sed '/5/,+3s/^/#/'
1
2
3
4
#5
#6
#7
#8
9
10
(11)注释指定多行
[root@rhel8 ~]# seq 5 |sed -r '/^3|^4/s/^/#/'
1
2
#3
#4
5
[root@rhel8 ~]# seq 5 |sed -r 's/^3|^4/#&/'
1
2
#3
#4
5
删除1-4行,并将剩余行的blp5替换为test
[root@rhel8 ~]# cat /tmp/services | sed -e '1,4d' -e 's/blp5/test/'
test 48129/tcp # Bloomberg locator
test 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
或
[root@rhel8 ~]# cat /tmp/services |sed '1,4d;s/blp5/test/'
test 48129/tcp # Bloomberg locator
test 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(1)在blp5上一行添加test
[root@rhel8 ~]# cat /tmp/services |sed '/blp5/i \test'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
test
blp5 48129/tcp # Bloomberg locator
test
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
[root@rhel8 ~]#
(2)在blp5下一行添加test
[root@rhel8 ~]# cat /tmp/services |sed '/blp5/a \test'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
test
blp5 48129/udp # Bloomberg locator
test
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(3)将blp5所在行替换为新行test
[root@rhel8 ~]# cat /tmp/services |sed '/blp5/c \test'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
test
test
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(4)在指定行的下一行添加一行
[root@rhel8 ~]# cat /tmp/services |sed '2a \test'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
test
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
(5)在指定行前面和后面添加一行
[root@rhel8 ~]# seq 5 |sed '3s/.*/txt\n&/'
1
2
txt
3
4
5
[root@rhel8 ~]# seq 5 |sed '3s/.*/&\ntest/'
1
2
3
test
4
5
[root@rhel8 ~]# cat a.txt
123
456
[root@rhel8 ~]# cat /tmp/services | sed '/blp5/r a.txt'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
123
456
blp5 48129/udp # Bloomberg locator
123
456
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
[root@rhel8 ~]# cat /tmp/services | sed '/blp5/w b.txt'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
[root@rhel8 ~]# cat abc.txt
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
n 读取下一行到模式空间。
N 追加下一行内容到模式空间,并以换行符\n 分隔。
(1)打印匹配的下一行
[root@rhel8 ~]# seq 5 |sed -n '/3/{n;p}'
4
(2)打印偶数
[root@rhel8 ~]# seq 6 |sed -n 'n;p'
2
4
6
sed 先读取第一行 1,执行 n 命令,获取下一行 2,此时模式空间是 2,执行 p 命令,打印模式空
间。 现在模式空间是 2,sed 再读取 3,执行 n 命令,获取下一行 4,此时模式空间为 4,执行 p 命
令,以此类推。
(3)打印奇数
[root@rhel8 ~]# seq 6 |sed -n 'n:d'
1
3
5
读取1,执行n,得出2,执行d,删除2,得空,以此类推,读取3,执行n,得出4,
执行d,删除4,得空,但是读取5时,因为n无法执行,所以d不执行。
因无-n参数,故输出1\n3\n5
[root@rhel8 ~]# seq 6 |sed -n 'p:n'
1
3
5
(4)每三行执行一次p命令
[root@rhel8 ~]# seq 6 |sed 'n;n;p'
1
2
3
3
4
5
6
6
sed 先读取第一行 1,并打印模式空间 1,执行 n 命令,获取下一行 2,并打印模式空间 2,再执行 n
命令,获取下一行 3,执行 p 命令,打印模式空间 3。sed 读取下一行 3,并打印模式空间 3,以此类
推。
(5)每三行替换一次
方法1:只是把p命令改成了替换命令
[root@rhel8 ~]# seq 6 |sed 'n;n;s/^/=/;s/$/=/'
1
2
=3=
4
5
=6=
方法2:这次用到了地址匹配,来实现上面的效果
[root@rhel8 ~]# seq 6 |sed '3~3{s/^/=/;s/$/=/}'
1
2
=3=
4
5
=6=
当执行多个 sed 命令时,有时相互会产生影响,我们可以用大括号{}把他们括起来
(6)再看下N命令的功能
[root@rhel8 ~]# seq 6 |sed 'N;q'
1
2
将两行合并一行:
[root@rhel8 ~]# seq 6 |sed 'N;s/\n//'
12
34
56
第一个命令:sed 读取第一行 1,N 命令读取下一行 2,并以\n2 追加,此时模式空间是 1\n2,再执
行 q 退出。
为了进一步说明 N 的功能,看第二个命令:执行 N 命令后,此时模式空间是 1\n2,再执行把\n 替换
为空,此时模式空间是 12,并打印。
[root@rhel8 ~]# seq 5 |sed -n 'N;p'
1
2
3
4
[root@rhel8 ~]# seq 6 |sed -n 'N;p'
1
2
3
4
5
6
为什么第一个不打印 5 呢?
因为 N 命令是读取下一行追加到 sed 读取的当前行,当 N 读取下一行没有内容时,则退出,也不会
执行 p 命令打印当前行。
当行数为偶数时,N 始终就能读到下一行,所以也会执行 p 命令。
[root@rhel8 ~]# seq 5 |sed -n '$!N;p'
1
2
3
4
5
加一个满足条件,当 sed 执行到最后一行时,用感叹号不去执行 N 命令,随后执行 p 命令
P(大写) 打印模式空间的第一行。
D 删除模式空间的第一行。
(1)打印奇数
[root@rhel8 ~]# seq 6 |sed -n 'N;P'
1
3
5
[root@rhel8 ~]# seq 6 |sed 'N;D'
6
读取第一行 1,执行 N 命令读取下一行并追加到模式空间,此时模式空间是 1\n2,执行 D 命令删除
模式空间第一行 1,剩余 2。
读取1,执行N,得出1\n2,执行D,得出2,执行N,得出2\n3,执行D,得出3,依此类推,得出6,
执行N,条件失败退出,因无-n参数,故输出6
h 复制模式空间内容到保持空间(覆盖)。
H 复制模式空间内容追加到保持空间。
g 复制保持空间内容到模式空间(覆盖)。
G 复制保持空间内容追加到模式空间。
x 模式空间与保持空间内容互换
(1)将匹配的内容覆盖到另一个匹配
[root@rhel8 ~]#seq 6 |sed -e '/3/{h;d}' -e '/5/g'
1
2
4
3
6
h 命令把匹配的 3 复制到保持空间,d 命令删除模式空间的 3。后面命令再对模式空间匹配 5,并用
g 命令把保持空间 3 覆盖模式空间 5。
(2)将匹配的内容放到最后
[root@rhel8 ~]# seq 6 |sed -e '/3/{h;d}' -e '$G'
1
2
4
5
6
3
(3)交换模式空间和保持空间
[root@rhel8 ~]#seq 6 |sed -e '/3/{h;d}' -e '/5/x' -e '$G'
1
2
4
3
6
5
在模式空间匹配 5 并将保持空间的 3 与 5 交换,5 就变成了 3,。最后把保持空间的 5
追加到模式空间的。
awk 是一个处理文本的编程语言工具,能用简短的程序处理标准输入或文件、数据排序、计算以及
生成报表等等。
awk 处理的工作方式与数据库类似,支持对记录和字段处理,这也是 grep 和 sed 不能实现的。
在 awk 中,缺省的情况下将文本文件中的一行视为一个记录,逐行放到内存中处理,而将一行中的
某一部分作为记录中的一个字段。用 1,2,3…数字的方式顺序的表示行(记录)中的不同字段。用
$后跟数字,引用对应的字段,以逗号分隔,0 表示整个行。
选项:
-f 从文件中读取awk程序源文件
-F 指定fs为输入字段分隔符
-v 变量赋值
–posix 兼容posix正则表达式
–dump-variables=[file] 把awk命令时的全局变量写入文件,默认文件是awkvars.out
–profile=[file] 格式化awk语句到文件,默认是awkprof.out
模式
常用模式有:
BUGIN{ } 给程序赋予初始状态,先执行的工作
END{ } 程序结束之后执行的一些扫尾工作
/regular expression/ 为每个输入记录匹配正则表达式
pattern && pattern 逻辑and,满足两个模式
pattern || pattern 逻辑or,满足其中一个模式
!pattern 逻辑not,不满足模式
pattern1,pattern2 范围模式,匹配所有模式1的记录,直到匹配到模式2
示例:
[root@rhel8 ~]# cat test.awk
{print $2}
[root@rhel8 ~]# cat /tmp/services | awk -f test.awk
48003/udp
48049/tcp
48128/tcp
48128/udp
48129/tcp
48129/udp
48556/tcp
48556/udp
48619/tcp
48619/udp
[root@rhel8 ~]# awk -F ':' '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
.......
还可以指定多个分隔符,作为同一个分隔符处理:
[root@rhel8 ~]# tail -n3 /tmp/services |awk -F'[/#]' '{print $3}'
com-bardac-dw
iqobject
iqobject
[root@rhel8 ~]# tail -n3 /tmp/services |awk -F'[/#]' '{print $1}'
com-bardac-dw 48556
iqobject 48619
iqobject 48619
[root@rhel8 ~]# tail -n3 /tmp/services |awk -F'[/#]' '{print $2}'
udp
tcp
udp
[root@rhel8 ~]# tail -n3 /tmp/services |awk -F'[ /]+' '{print $2}'
48556
48619
48619
[]元字符的意思是符号其中任意一个字符,也就是说每遇到一个/或#时就分隔一个字段,当用多个
分隔符时,就能更方面处理字段了
[root@rhel8 ~]# awk -v a=123 'BEGIN{print a}'
123
系统变量作为awk变量的值
[root@rhel8 ~]# a=123
[root@rhel8 ~]# awk -v a=$a 'BEGIN{print a}'
123
或使用单引号
[root@rhel8 ~]# awk 'BEGIN{print '$a'}'
123
[root@rhel8 ~]#
[root@rhel8 ~]# seq 5 |awk --dump-variables '{print $0}'
1
2
3
4
5
[root@rhel8 ~]# cat awkvars.out
ARGC: 1
ARGIND: 0
ARGV: array, 1 elements
BINMODE: 0
CONVFMT: "%.6g"
ENVIRON: array, 29 elements
ERRNO: ""
FIELDWIDTHS: ""
FILENAME: "-"
FNR: 5
FPAT: "[^[:space:]]+"
FS: " "
FUNCTAB: array, 41 elements
IGNORECASE: 0
LINT: 0
NF: 1
NR: 5
OFMT: "%.6g"
OFS: " "
ORS: "\n"
PREC: 53
PROCINFO: array, 20 elements
RLENGTH: 0
ROUNDMODE: "N"
RS: "\n"
RSTART: 0
RT: "\n"
SUBSEP: "\034"
SYMTAB: array, 28 elements
TEXTDOMAIN: "messages"
[root@rhel8 ~]#
BEGIN 模式是在处理文件之前执行该操作,常用于修改内置变量、变量赋值和打印输出的页眉或标题。
例如:打印页眉
[root@rhel8 ~]# tail /tmp/services |awk 'BEGIN{print "Service\t\tPort\t\t\tDescription\n==="}{print $0}'
Service Port Description
===
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
END 模式是在程序处理完才会执行。
例如:打印页尾
[root@rhel8 ~]# tail /tmp/services |awk '{print $0}END{print "===\nEND......"}'
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
===
END......
[root@rhel8 ~]# tail /tmp/services |awk --profile 'BEGIN{print"Service\t\tPort\t\t\tDescription\n==="}{print $0}END{print "===\nEND......"}'
Service Port Description
===
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
===
END......
[root@rhel8 ~]# cat awkprof.out
# gawk profile, created Thu Sep 15 07:45:12 2022
# BEGIN rule(s)
BEGIN {
print "Service\t\tPort\t\t\tDescription\n==="
}
# Rule(s)
{
print $0
}
# END rule(s)
END {
print "===\nEND......"
}
匹配包含 tcp 的行:
[root@rhel8 ~]# cat /tmp/services |awk '/tcp/{print $0}'
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
iqobject 48619/tcp # iqobject
匹配记录中包含 blp5 和 tcp 的行:
[root@rhel8 ~]# cat /tmp/services |awk '/blp5/ && /tcp/{print $0}'
blp5 48129/tcp # Bloomberg locator
匹配记录中包含 blp5 或 tcp 的行:
[root@rhel8 ~]# cat /tmp/services |awk '/blp5/ || /tcp/{print $0}'
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
iqobject 48619/tcp # iqobject
不匹配开头是#和空行:
[root@rhel8 ~]# awk '! /^#/ && ! /^$/{print $0}' /etc/httpd/conf/httpd.conf
或者
[root@rhel8 ~]# awk '! /^#|^$/' /etc/httpd/conf/httpd.conf
[root@rhel8 ~]# cat /tmp/services |awk '/^blp5/,/^com/'
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
用自己的语言表述该命令的含义
1、[root@rhel8 ~]# cat /tmp/services | sed -n '$!p'
除了这个文件的最后一行不打印,其他都打印
2、[root@rhel8 ~]# cat /tmp/services | sed -n '/^blp5/,/^iqo/p'
找到此文件中以blp5开头的到以iqo结尾的行打印。此处有优先级比如:
原文件
[root@rhel8 ~]# cat /tmp/services
nimgtw 48003/udp # Nimbus Gateway
3gpp-cbsp 48049/tcp # 3GPP Cell Broadcast Service Protocol
isnetserv 48128/tcp # Image Systems Network Services
isnetserv 48128/udp # Image Systems Network Services
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
查找后
[root@rhel8 ~]# cat /tmp/services | sed -n '/^blp5/,/^com/p'
blp5 48129/tcp # Bloomberg locator
blp5 48129/udp # Bloomberg locator
com-bardac-dw 48556/tcp # com-bardac-dw
3、[root@rhel8 ~]# cat /tmp/services | sed '2~2d'
删除此文件的偶数行
4、[root@rhel8 ~]# cat /tmp/services | sed -r 's/(.*)(\<[0-9]+\>)\/(tcp|udp)(.*)/\1\3\/\2\4/'
将协议与端口号位置互换
使用正则的方式把第二列的内容和第三列的内容互换
5、[root@rhel8 ~]# seq 5 |sed '3s/.*/&\ntest/'
引用第三行,在第三行后写入test
6、[root@rhel8 ~]# seq 8 |sed -n 'p;n'
从第一行开始往后没隔一行打印一行
7、[root@rhel8 ~]# seq 8 |sed 'N;s/\n//'
[root@sb ~]# seq 8 |sed 'N'
1
2
3
4
5
6
7
8
[root@sb ~]# seq 8 |sed 'N;s/\n//'
12
34
56
78
[root@sb ~]#
以第一行为例: sed 读取第一行1,N 命令读取下一行2,形成一个新的模式空间
8、[root@rhel8 ~]# tail -n3 /tmp/services |awk -F'[/#]' '{print $2}'
查看此文件的最后三行,指定以 / # 为字段分隔符,打印此分隔符的第二个字段
9、指定冒号:为分隔符,打印用户的shell环境字段(使用awk)
[root@sb ~]# awk -F ':' '{print $1,$7}' /etc/passwd
10、[root@rhel8 ~]# tail /tmp/services |awk --profile 'BEGIN{print"Service\t\tPort\t\t\tDescription\n----"}{print $0}END{print "---\nEND......"}'
打印此文件以(Service Port Description开头,他的下一行为----),此文件倒数第二行为----,此文件行尾为END......结尾