成都全美网站建设,上海徐家汇网站建设,哪个网站有工笔教程,做网络推广的方法shell文本处理
一、grep
过滤来自一个文件或标准输入匹配模式内容。除了 grep 外#xff0c;还有 egrep、fgrep。egrep 是 grep 的扩展#xff0c;相当于 grep -E。fgrep 相当于 grep -f#xff0c;用的比较少。 用法 grep [OPTION]... PATTERN [FILE]...支持的正则描述…shell文本处理
一、grep
过滤来自一个文件或标准输入匹配模式内容。除了 grep 外还有 egrep、fgrep。egrep 是 grep 的扩展相当于 grep -E。fgrep 相当于 grep -f用的比较少。 用法 grep [OPTION]... PATTERN [FILE]...支持的正则描述-E–extended-regexp模式是扩展正则表达式ERE-P–perl-regexp模式是 Perl 正则表达式-e–regexpPATTERN使用模式匹配可指定多个模式匹配-f–fileFILE从文件每一行获取匹配模式-i–ignore-case忽略大小写-w–word-regexp模式匹配整个单词-x–line-regexp模式匹配整行-v–invert-match打印不匹配的行 输出控制描述-m–max-countNUM输出匹配的结果 num 数-n–line-number打印行号-H–with-filename打印每个匹配的文件名-h–no-filename不输出文件名-o–only-matching只打印匹配的内容-q–quiet不输出正常信息-s, --no-messages不输出错误信息-r–recursive递归目录-c–count只打印每个文件匹配的行数–includeFILE_PATTERN只检索匹配的文件–excludeFILE_PATTERN跳过匹配的文件–exclude-fromFILE跳过匹配的文件来自文件模式–exclude-dirPATTERN跳过匹配的目录 内容行控制描述-B–before-contextNUM打印匹配的前几行-A–after-contextNUM打印匹配的后几行-C–contextNUM打印匹配的前后几行–color[WHEN]匹配的字体颜色 示例: 输出 b 文件中在 a 文件相同的行 # grep -f a b输出 b 文件中在 a 文件不同的行 # grep -v -f a b匹配多个模式 # echo a bc de |xargs -n1 |grep -e a -e bc
a
bc去除空格 http.conf 文件空行或开头#号的行 # grep -E -v ^$|^# /etc/httpd/conf/httpd.conf匹配开头不分大小写的单词 # echo A a b c |xargs -n1 |grep -i a
或
# echo A a b c |xargs -n1 |grep [Aa]
A
a只显示匹配的字符串 # echo this is a test |grep -o is
is
is输出匹配的前五个结果 # seq 1 20 |grep -m 5 -E [0-9]{2}
10
11
12
13
14统计匹配多少行 # seq 1 20 |grep -c -E [0-9]{2}
11匹配 b 字符开头的行 # echo a bc de |xargs -n1 |grep ^bbc匹配 de 字符结尾的行并输出匹配的行 # echo a ab abc abcd abcde |xargs -n1 |grep -n de$
5:abcde递归搜索/etc 目录下包含 ip 的 conf 后缀文件 # grep -r 192.167.1.1 /etc --include *.conf排除搜索 bak 后缀的文件 # grep -r 192.167.1.1 /opt --exclude *.bak排除来自 file 中的文件 # grep -r 192.167.1.1 /opt --exclude-from file匹配 41 或 42 的数字 # seq 41 45 |grep -E 4[12]
41
42匹配至少 2 个字符 # seq 13 |grep -E [0-9]{2}
10
11
12
13匹配至少 2 个字符的单词最多 3 个字符的单词 # echo a ab abc abcd abcde |xargs -n1 |grep -E -w -o [a-z]{2,3}
ab
abc匹配所有 IP # ifconfig |grep -E -o [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}
# ifconfig |grep -E -o [0-9]{1,3}\.{3}[0-9]{1,3}
# ifconfig | grep -E -o ((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)打印匹配结果及后 3 行 # seq 1 10 |grep 5 -A 3
5
6
7
8打印匹配结果及前 3 行 # seq 1 10 |grep 5 -B 3
2
3
45打印匹配结果及前后 3 行 # seq 1 10 |grep 5 -C 3
2
3
4
5
6
7
8不显示输出 ## 不显示错误输出
# grep a abc
grep: abc: No such file or directory
# grep -s a abc
# echo $?
2
## 不显示正常输出
# grep -q a a.txt二、sed
流编辑器过滤和替换文本。
工作原理sed 命令将当前处理的行读入模式空间进行处理处理完把结果输出并清空模式空间。然后再将下一行读入模式空间进行处理输出以此类推直到最后一行。还有一个空间叫保持空间又称暂存空间可以暂时存放一些处理的数据但不能直接输出只能放到模式空间输出。这两个空间其实就是在内存中初始化的一个内存区域存放正在处理的数据和临时存放的数据。 用法 sed [OPTION]... {script-only-if-no-other-script} [input-file]...
sed [选项] 地址 命令 file选项描述-n不打印模式空间-e执行脚本、表达式来处理-f执行动作从文件读取执行-i修改原文件-r使用扩展正则表达式 命令描述s/regexp/replacement/替换字符串p打印当前模式空间P打印模式空间的第一行d删除模式空间开始下一个循环D删除模式空间的第一行开始下一个循环打印当前行号a \text当前行追加文本i \text当前行上面插入文本c \text所选行替换新文本q立即退出 sed 脚本r追加文本来自文件: labellabel 为 b 和 t 命令b label分支到脚本中带有标签的位置如果分支不存在则分支到脚本的末尾t label如果 s///是一个成功的替换才跳转到标签h H复制/追加模式空间到保持空间g G复制/追加保持空间到模式空间x交换模式空间和保持空间内容l打印模式空间的行并显示控制字符$n N读取/追加下一行输入到模式空间w filename写入当前模式空间到文件!取反、否定引用已匹配字符串 地址描述first~step步长每 step 行从第 first 开始$匹配最后一行/regexp/正则表达式匹配行number只匹配指定行addr1,addr2开始匹配 addr1 行开始直接 addr2 行结束addr1,N从 addr1 行开始向后的 N 行addr1,~N从 addr1 行开始到 N 行结束 匹配打印 借助以下文本内容作为示例讲解 # tail /etc/services
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures打印匹配axio开头的行 # tail /etc/services | sed -n /^axio/p
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol打印第一行 # tail /etc/services | sed -n 1 p
aigairserver 21221/tcp # Services for Air Server打印第一行至第三行 # tail /etc/services | sed -n 1,3 p
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery打印奇数行 tail /etc/services | sed -n 1~2 p
aigairserver 21221/tcp # Services for Air Server
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
axio-disc 35100/tcp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System 打印匹配行及后一行 # tail /etc/services | sed -n /pmw/,1 p
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive 打印最后一行 # tail /etc/services | sed -n $ p
spremotetablet 46998/tcp # Capture handwritten signatures 不打印最会一行 # tail /etc/services | sed -n $ !p
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System 匹配范围 # tail /etc/services | sed -n /^axio/,/^pmw/ p
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API匹配开头行到最后一行 # tail /etc/services | sed -n /^axio/,$ p
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures 引用系统变量用引号 # tail /etc/services | sed -n $a,3 p
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
# tail /etc/services | sed -n $a,3 p
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery注意 sed 命令用单引号时里面变量用单引号引起来或者 sed 命令用双引号因为双引号解释特殊符号原有意义。感叹号也就是对后面的命令取反。 匹配删除 打印是把匹配的打印出来删除是把匹配的删除删除只是不用-n 选项。 # tail /etc/services | sed /axio/ d
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures# tail /etc/services | sed 1 d
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures# tail /etc/services | sed 1~2 d
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/udp # Axiomatic discovery protocol
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
spremotetablet 46998/tcp # Capture handwritten signatures# tail /etc/services | sed 1,3 d
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures## 去除空格http.conf文件空行或开头#号的行
# sed /^#/d;/^$/d /etc/httpd/conf/httpd.conf替换 s/ / / 替换axio-disc字符串为test # tail /etc/services | sed s/axio-disc/test/
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
test 35100/tcp # Axiomatic discovery protocol
test 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures替换开头是axio-disc的字符串并打印 # tail /etc/services | sed -n s/^axio-disc/test/ p
test 35100/tcp # Axiomatic discovery protocol
test 35100/udp # Axiomatic discovery protocol使用命令引用匹配内容并替换 # tail /etc/services | sed s/21221/.0/
aigairserver 21221.0/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signaturesIP加引号 echo 172.25.254.100 172.25.254.101 172.25.254.102 | sed -r s/[^ ]//g
172.25.254.100 172.25.254.101 172.25.254.102对1-4行的ka-kdp进行替换 # tail /etc/services | sed 1,4s/ka-kdp/test/
aigairserver 21221/tcp # Services for Air Server
test 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures 对匹配行进行替换 tail /etc/services | sed /21221\/tcp/s/aigairserver/test/
test 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures二次匹配替换 # tail /etc/services | sed /21221\/tcp/s/aigairserver/test/;s/ka-kdp/test/
test 21221/tcp # Services for Air Server
test 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures分组使用在每个字符串后面添加123 # tail /etc/services | sed -r s/(.*) (.*)(#.*)/\1\2test \3/
aigairserver 21221/tcp test # Services for Air Server
ka-kdp 31016/udp test # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp test # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp test # dhanalakshmi.org EDI Service
axio-disc 35100/tcp test # Axiomatic discovery protocol
axio-disc 35100/udp test # Axiomatic discovery protocol
pmwebapi 44323/tcp test # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp test # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp test # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp test # Capture handwritten signatures将协议与端口号位置调换 # tail /etc/services | sed -r s/(.*)(\[0-9]\)\/(tcp|udp)(.*)/\1\3\/\2\4/
aigairserver tcp/21221 # Services for Air Server
ka-kdp udp/31016 # Kollective Agent Kollective Delivery
ka-sddp tcp/31016 # Kollective Agent Secure Distributed Delivery
edi_service udp/34567 # dhanalakshmi.org EDI Service
axio-disc tcp/35100 # Axiomatic discovery protocol
axio-disc udp/35100 # Axiomatic discovery protocol
pmwebapi tcp/44323 # Performance Co-Pilot client HTTP API
cloudcheck-ping udp/45514 # ASSIA CloudCheck WiFi Management keepalive
cloudcheck tcp/45514 # ASSIA CloudCheck WiFi Management System
spremotetablet tcp/46998 # Capture handwritten signatures位置调换 ## 替换x字符为大写
# echo abc cde xyz | sed -r s/(.*)x/\1X/
abc cde Xyz
## 456与cde调换
# echo abc:cde;123:456 | sed -r s/([^:])(;.*:)([^:]$)/\3\2\1/
abc:456;123:cde注释匹配行后的多少行 # seq 10 | sed /5/,3s/^/#/
1
2
3
4
#5
#6
#7
#8
9
10注释指定多行 # seq 5 | sed -r /^3|^4/s/^/#/
1
2
#3
#4
5
# seq 5 | sed -r s/^3|^4/#\0/
1
2
#3
#4
5去除开头和结尾空格或制表符 # echo 1 2 3 |sed s/^[ \t]]*//;s/[ \t]*$//
1 2 3多重编辑-e # tail /etc/services | sed -e 1,2d -e s/axio-disc/test/
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
test 35100/tcp # Axiomatic discovery protocol
test 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures## 也可以使用分号分隔
# tail /etc/services | sed 1,2d;s/axio-disc/test/
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
test 35100/tcp # Axiomatic discovery protocol
test 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures添加新内容a、i和c 在axio-disc上一行添加test # tail /etc/services | sed /axio-disc/i \test
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
test
axio-disc 35100/tcp # Axiomatic discovery protocol
test
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures在axio-disc下一行添加test # tail /etc/services | sed /axio-disc/a \test
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
test
axio-disc 35100/udp # Axiomatic discovery protocol
test
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures将axio-disc替换新行 # tail /etc/services | sed /axio-disc/c \test
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
test
test
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures 在指定行下一行添加一行 # tail /etc/services | sed 2a \test
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
test
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures在指定行的前面和后面添加一行 # seq 5 | sed 3s/.*/txt\n/
1
2
txt
3
4
5# seq 5 | sed 3s/.*/\ntxt/
1
2
3
txt
4
5读取文件并追加到匹配行后r # cat a.txt
123
456# tail /etc/services | sed /axio-disc/r a.txt
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
123
456
axio-disc 35100/udp # Axiomatic discovery protocol
123
456
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures将匹配行写入文件w # tail /etc/services | sed /axio-disc/w b.txt
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures
# cat b.txt
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol读取下一行n和N n读取下一行到模式空间。 N追加下一行内容到模式空间并以换行符\n分隔。 打印匹配的下一行 seq 5 | sed -n /3/{n;p}
4打印偶数 # seq 6 | sed -n n;p
2
4
6
## sed 先读取第一行 1执行 n 命令获取下一行 2此时模式空间是 2执行 p 命令打印模式空间。 现在模式空间是 2sed 再读取 3执行 n 命令获取下一行 4此时模式空间为 4执行 p 命令以此类推。打印奇数 # seq 6 | sed n;d
1
3
5
## sed 先读取第一行 1此时模式空间是 1并打印模式空间 1执行 n 命令获取下一行 2执行 d命令删除模式空间的 2sed 再读取 3此时模式空间是 3并打印模式空间再执行 n 命令获取下一行 4执行 d 命令删除模式空间的 3以此类推。每三行执行一次p命令 # 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,以此类推。每三行替换一次 ## 我们只是把 p 命令改成了替换命令。
# seq 6 | sed n;n;s/^//;s/$//
1
2
3
4
5
6
## 这次用到了地址匹配来实现上面的效果当执行多个 sed 命令时有时相互会产生影响我们可以用大括号{}把他们括起来。
# seq 6 | sed 3~3{s/^//;s/$//}
1
2
3
4
5
6
## 当执行多个 sed 命令时有时相互会产生影响我们可以用大括号{}把他们括起来N:追加下一行内容到模式空间并以换行符\n分隔。 ## sed 读取第一行 1N 命令读取下一行 2并以\n2 追加此时模式空间是 1\n2再执
行 q 退出。
# seq 6 | sed N;q
1
2## 执行 N 命令后此时模式空间是 1\n2再执行把\n 替换为空此时模式空间是 12并打印
# seq 6 | sed N;s/\n//
12
34
56## N 命令是读取下一行追加到 sed 读取的当前行当 N 读取下一行没有内容时则退出也不会执行 p 命令打印当前行,所以不会打印5。
# seq 5 | sed -n N;p
1
2
3
4
# seq 6 | sed -n N;p
1
2
3
4
5
6## 打印奇数行的最后一行加一个满足条件当 sed 执行到最后一行时用感叹号不去执行 N 命令随后执行 p 命令。
# seq 5 | sed -n $!N;p
1
2
3
4
5打印和删除模式空间的第一行P和D P打印模式空间的第一行。 D删除模式空间的第一行。 打印奇数 # seq 6 | sed -n N;P
1
3
5保留最后一行 # seq 6 | sed N;D
6
## 读取第一行 1执行 N 命令读取下一行并追加到模式空间此时模式空间是 1\n2执行 D 命令删除模式空间第一行 1剩余 2。
读取第二行执行 N 命令此时模式空间是 3\n4执行 D 命令删除模式空间第一行 3剩余 4。以此类推读取最后一行打印时而 N 获取不到下一行则退出不再执行 D因此模式空间只剩余 6就打印。保持空间操作h与H、g与G和x h复制模式空间内容到保持空间覆盖。 H复制模式空间内容追加到保持空间。 g复制保持空间内容到模式空间覆盖。 G复制保持空间内容追加到模式空间。 x模式空间与保持空间内容互换。 将匹配的内容覆盖到另一个匹配 # seq 6 | sed -e /3/{h;d} -e /5/g
1
2
4
3
6
## h 命令把匹配的 3 复制到保持空间d 命令删除模式空间的 3。后面命令再对模式空间匹配 5并用g 命令把保持空间 3 覆盖模式空间 5。将匹配的内容放到最后 # seq 6 | sed -e /3/{h;d} -e $G
1
2
4
5
6
3交换模式空间和保持空间 # seq 6 | sed -e /3/{h;d} -e /5/x -e $G
1
2
4
3
6
5倒叙输出 # seq 5 | sed 1!G;h;$!d
5
4
3
2
1
## 1!G 第一行不执行把保持空间内容追加到模式空间因为现在保持空间还没有数据。
h 将模式空间放到保持空间暂存。
$!d 最后一行不执行删除模式空间的内容。
读取第一行 1 时跳过 G 命令执行 h 命令将模式空间 1 复制到保持空间执行 d 命令删除模式空间的 1。
读取第二行 2 时模式空间是 2执行 G 命令将保持空间 1 追加到模式空间此时模式空间是2\n1执行 h 命令将 2\n1 覆盖到保持空间d 删除模式空间。
读取第三行 3 时模式空间是 3执行 G 命令将保持空间 2\n1 追加到模式空间此时模式空间是3\n2\n1执行 h 命令将模式空间内容复制到保持空间d 删除模式空间。
以此类推读到第 5 行时模式空间是 5执行 G 命令将保持空间的 4\n3\n2\n1 追加模式空间然后复制到模式空间5\n4\n3\n2\n1不执行 d模式空间保留输出。
由此可见每次读取的行先放到模式空间再复制到保持空间d 命令删除模式空间内容防止输出再追加到模式空间因为追加到模式空间会追加到新读取的一行的后面循环这样操作 就把所有行一行行追加到新读取行的后面就形成了倒叙。每行后面添加新空行 # seq 5 | sed G
12345 打印匹配行的上一行 # seq 5 | sed -n /3/{x;p};h
2
## 读取第一行 1没有匹配到 3不执行{x;p}执行 h 命令将模式空间内容 1 覆盖到保持空间。
读取第二行 2没有匹配到 3不执行{x;p}执行 h 命令将模式空间内容 2 覆盖到保持空间。
读取第三行 3匹配到 3执行 x 命令把模式空间 3 与保持空间 2 交换再执行 p 打印模式空间 2.以此类推。打印匹配行到最后一行或下一行到最后一行 ## 打印匹配行到最后一行
# seq 5 |sed -n /3/,$p
3
4
5
# seq 5 |sed -n /3/,${h;x;p}
3
4
5
# seq 5 |sed -n /3/{:a;N;$!ba;p}
3
4
5
## 打印匹配行的下一行到最后一行
# seq 5 |sed -n /3/{n;:a;N;$!ba;p}
4
5## 匹配到 3 时n 读取下一行 4此时模式空间是 4执行 N 命令读取下一行并追加到模式空间此时模式空间是 4\n5标签循环完成后打印模式空间 4\n5。标签、b和t 标签可以控制流实现分支判断。 lable name 定义标签。 b lable 跳转到指定标签如果没有标签则到脚本末尾。 t label 跳转到指定标签前提是s///命令执行成功。 将换行符替换成逗号 ## 方法1
# seq 6 | sed N;s/\n/,/
1,2
3,4
5,6
## 这种方式并不能满足我们的需求每次 sed 读取到模式空间再打印是新行替换\n 也只能对 N 命令追加后的 1\n2 这样替换。## 使用标签:a 是定义的标签名b a 是跳转到 a 位置。
sed 读取第一行 1N 命令读取下一行 2此时模式空间是 1\n2$执行替换此时模式空间是1,2$执行 b 命令再跳转到标签 a 位置继续执行 N 命令读取下一行 3 追加到模式空间此时模式空间是 1,2\n3$再替换以此类推不断追加替换直到最后一行 N 读不到下一行内容退出。
# seq 6 | sed :a;N;s/\n/,/;b a
1,2,3,4,5,6## 方法2先将每行读入到模式空间最后再执行全局替换。$!是如果是最后一行则不执行 b a 跳转最后执行全局替换。
# seq 6 | sed :a;N;$!b a;s/\n/,/g
1,2,3,4,5,6 每三个数字加一个逗号 # echo 123456789 | sed -r :a ;s/([0-9])([0-9]{3})/\1,\2/;t a
123,456,789
## 执行第一次时替换最后一个跳转后再对 123456 匹配替换直到匹配替换不成功不执行 t 命令。忽略大小写匹配I # echo -e a\nA\nb\nc | sed s/a/1/Ig
1
1
b
c获取总行数$ # seq 10 |sed -n $三、awk
awk 是一个处理文本的编程语言工具能用简短的程序处理标准输入或文件、数据排序、计算以及生成报表等等。
在 Linux 系统下默认 awk 是 gawk它是 awk 的 GNU 版本。可以通过命令查看应用的版本ls -l /bin/awk
awk 处理的工作方式与数据库类似支持对记录和字段处理这也是 grep 和 sed 不能实现的。
在 awk 中缺省的情况下将文本文件中的一行视为一个记录逐行放到内存中处理而将一行中的某一部分作为记录中的一个字段。用 1,2,3…数字的方式顺序的表示行记录中的不同字段。用$后跟数字引用对应的字段以逗号分隔0 表示整个行。 基本语法命令 awk option pattern {action} file
其中 pattern 表示 AWK 在数据中查找的内容而 action 是在找到匹配内容时所执行的一系列命令。花括号用于根据特定的模式对一系列指令进行分组。选项 选项描述-f program-file从文件中读取 awk 程序脚本源文件-F fs指定 fs 为输入字段分隔符-v varvalue变量赋值–posix兼容 POSIX 正则表达式–dump-variables[file]把 awk 命令时的全局变量写入文件默认文件是awkvars.out–profile[file]格式化 awk 语句到文件默认是 awkprof.out 模式 PatternDescriptionBEGIN{ }给程序赋予初始状态先执行的工作END{ }程序结束之后执行的一些扫尾工作/regular expression/为每个输入记录匹配正则表达式pattern pattern逻辑 and满足两个模式pattern || pattern逻辑 or满足其中一个模式! pattern逻辑 not不满足模式pattern1, pattern2范围模式匹配所有模式 1 的记录直到匹配到模式 2 动作print、流程控制、I/O语句等 从文件读取awk程序处理文件 # vim test.awk
{pringt $2}
# tail -n3 /etc/services | awk -f test.awk
45514/udp
45514/tcp
46998/tcp指定分隔符打印指定字段 ## 打印第二个字段默认以空格分隔
# tail -n3 /etc/services | awk {print $2}
45514/udp
45514/tcp
46998/tcp
## 指定冒号为分隔符打印第一字段
# awk -F : {print $1} /etc/passwd
root
bin
daemon
adm
lp
sync
......指定多个分隔符作为同一个分隔符处理 # tail -n3 /etc/services | awk -F[/#] {print $3}ASSIA CloudCheck WiFi Management keepaliveASSIA CloudCheck WiFi Management SystemCapture handwritten signatures# tail -n3 /etc/services | awk -F[ /] {print $1}
cloudcheck-ping
cloudcheck
spremotetablet# tail -n3 /etc/services | awk -F/ {print $1}
cloudcheck-ping 45514
cloudcheck 45514
spremotetablet 46998变量赋值 # awk -v a123 BEGIN{print a}
123
## 系统变量作为awk变量的值
# a123
# awk -v a$a BEGIN{print a}
123
## 使用单引号
# awk BEGIN{print $a}
123输出awk全局变量到文件 # seq 5 | awk --dump-variables {print $0}
1
2
3
4
5
# cat awkvars.out
ARGC: 1
ARGIND: 0
ARGV: array, 1 elements
BINMODE: 0
CONVFMT: %.6g
ENVIRON: array, 27 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, 21 elements
RLENGTH: 0
ROUNDMODE: N
RS: \n
RSTART: 0
RT: \n
SUBSEP: \034
SYMTAB: array, 28 elements
TEXTDOMAIN: messages BEGIN 和 END BEGINBEGIN 模式是在处理文件之前执行该操作常用于修改内置变量、变量赋值和打印输出的页眉或标题。 ## 打印页眉
# tail /etc/services | awk BEGIN{print Service\t\tPort\t\t\tDescription\n}{print $0}
Service Port Descriptionaigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signaturesENDEND 模式是在程序处理完才会执行。 ## 打印页尾
# tail /etc/services | awk {print $0}END{print \nEND......}
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signaturesEND......格式化输出awk命令到文件 # tail /etc/services | awk --profile BEGIN{print Service\t\tPort\t\t\tDescription\n}{print $0}END{print \nEND......}
Service Port Descriptionaigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signaturesEND......
# cat awkprof.out# gawk profile, created Sun Feb 9 05:17:03 2025# BEGIN rule(s)BEGIN {1 print Service\t\tPort\t\t\tDescription\n}# Rule(s)10 {10 print $0}# END rule(s)END {1 print \nEND......}/re/正则匹配 # tail /etc/services | awk /tcp/{print $0}
aigairserver 21221/tcp # Services for Air Server
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
axio-disc 35100/tcp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures逻辑and、or和not 匹配记录中包含 axio 和 tcp 的行 # tail /etc/services | awk /axio/ /tcp/{print $0}
axio-disc 35100/tcp # Axiomatic discovery protocol匹配记录中包含axio 或 tcp 的行 # tail /etc/services | awk /axio/ || /tcp/{print $0}
aigairserver 21221/tcp # Services for Air Server
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures不匹配开头是#和空行 # awk ! /^#/ ! /^$/{print $0} /etc/httpd/conf/httpd.conf
# awk /^[^#]/ !/^$/ /etc/httpd/conf/httpd.conf
# awk /^[^#] | ^$/ /etc/httpd/conf/httpd.conf匹配范围 # tail /etc/services | awk /^axio/,/^cloudcheck/
axio-disc 35100/tcp # Axiomatic discovery protocol
axio-disc 35100/udp # Axiomatic discovery protocol
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive## 对匹配范围后记录再次处理例如匹配关键字下一行到最后一行
# seq 5 |awk /3/,/^$/{printf /3/?:$0\n}
4
5# seq 5 | awk /3/{t1;next}t
4
5
## 1 和 2 都不匹配 3不执行后面{}执行 tt 变量还没赋值为空空在 awk 中就为假就不打印当前行。匹配到 3执行 t1next 跳出不执行 t。4 也不匹配 3执行 tt 的值上次赋值的 1为真打印当前行以此类推。非 0 的数字都为真所以 t 可以写任意非 0 数字如果想打印匹配行都最后一行就可以这样了
# seq 5 | awk /3/{t1}t
3
4
5内置变量 变量名描述FSOFS输入字段分隔符默认是空格或制表符输出字段分隔符默认是空格RSORS输入记录分隔符默认是换行符\n输出记录分隔符默认是换行符\nNFNR统计当前记录中字段个数统计记录编号每处理一行记录编号就会1FNR统计记录编号每处理一行记录编号也会1与 NR 不同的是处理第二个文件时编号会重新计数。ARGC命令行参数数量ARGV命令行参数数组序列数组下标从 0 开始ARGV[0]是 awkARGIND当前正在处理的文件索引值。第一个文件是 1第二个文件是 2以此类推EVNIRON当前系统的环境变量FILENAME输出当前处理的文件名IGNORECASE忽略大小写SUBSEP数组中下标的分隔符默认为\034 示例 FS和OFS ## 在程序开始前重新赋值 FS 变量改变默认分隔符为冒号与-F 一样。
# awk BEGIN{FS:}{print $1,$2} /etc/passwd | head -5
root x
bin x
daemon x
adm x
lp x## 也可以使用-v来重新赋值这个变量
# awk -vFS: {print $1,$2} /etc/passwd | head -5
root x
bin x
daemon x
adm x
lp x##由于OFS默认是以空格分隔反向引用多个字段分隔也是空格如果想指定输出分隔符这样
# awk BEGIN{FS:;OFS:}{print $1,$2} /etc/passwd | head -n5
root:x
bin:x
daemon:x
adm:x
lp:x## 也可以通过字符串拼接实现分隔awk BEGIN{FS:}{print $1#$2} /etc/passwd|head -5
root#x
bin#x
daemon#x
adm#x
lp#xRS和ORS ## RS默认是\n分隔每行如果想指定以某个字符作为分隔符来处理记录
# echo www.baidu.com/user/test.html | awk BEGIN{RS/}{print $0}
www.baidu.com
user
test.html## RS也支持正则简单演示
# seq -f str%02g 10 | sed n;n;a\---| awk BEGIN{RS-}{print $1}
str01
str04
str07
str10## 将输出的换行符替换为号
# seq 10 | awk BEGIN{ORS}{print $0}
12345678910##替换某个字符
# tail -n2 /etc/services | awk BEGIN{RS/;ORS#}{print $0}
cloudcheck 45514#tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998#tcp # Capture handwritten signaturesNF:NF是字段个数 # echo a b c d e f|awk {print NF}
6## 打印最后一个字段
# echo a b c d e f|awk {print $NF}
f## 打印倒数第二个字段
# echo a b c d e f|awk {print $(NF-1)}
e## 排除最后两个字段
# echo a b c d e f|awk {$NF;$(NF-1);print $0}
a b c d## 排除第一个字段
# echo a b c d e f|awk {$1;print $0}b c d e fNR和FNR NR 统计记录编号每处理一行记录编号就会1FNR 不同的是在统计第二个文件时会重新计数。 ## 打印行数
# tail -n5 /etc/services|awk {print NR,$0}
1 axio-disc 35100/udp # Axiomatic discovery protocol
2 pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
3 cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
4 cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
5 spremotetablet 46998/tcp # Capture handwritten signatures## 打印总行数
# tail -n5 /etc/services|awk END{print NR}
5
## 打印第三行
# tail -n5 /etc/services|awk NR3
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
## 打印第三行的第二个字段
# tail -n5 /etc/services|awk NR3{print $2}
45514/udp
## 打印前三行
# tail -n5 /etc/services|awk NR3{print NR,$0}
1 axio-disc 35100/udp # Axiomatic discovery protocol
2 pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
3 cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive 接下来我们来看看NR和FNR的区别 [rootopenEulter-1 ~]# cat a.txt
a
b
c
[rootopenEulter-1 ~]# cat b.txt
c
d
e
[rootopenEulter-1 ~]# awk {print NR,FNR,$0} a.txt b.txt
1 1 a
2 2 b
3 3 c
4 1 c
5 2 d
6 3 e
## 我们可以发现NR 每处理一行就会1而 FNR 在处理第二个文件时编号重新计数。同时也知道 awk 处理两个文件时是合并到一起处理。
[rootopenEulter-1 ~]# awk FNRNR{print $01}FNR!NR{print $02} a.txt b.txt
a1
b1
c1
c2
d2
e2
## 当 FNRNR 时说明在处理第一个文件内容不等于时说明在处理第二个文件内容。ARGC和ARGV ARGC是命令行参数数量 ARGV是将命令行参数存到数组元素由ARGC指定数组下标从0开始 [rootopenEulter-1 ~]# awk BEGIN{print ARGC} 1 2 3
4
[rootopenEulter-1 ~]# awk BEGIN{print ARGV[0]}
awk
[rootopenEulter-1 ~]# awk BEGIN{print ARGV[1]} 1 2
1
[rootopenEulter-1 ~]# awk BEGIN{print ARGV[2]} 1 2
2 ARGIND ARGIND是当前正在处理的文件索引值第一个文件是1第二个文件是2以此类推从而可以通过这种方式判断正在处理哪个文件。 [rootopenEulter-1 ~]# awk {print ARGIND,$0} a.txt b.txt
1 a
1 b
1 c
2 c
2 d
2 e
[rootopenEulter-1 ~]# awk ARGIND1{print a-$0}ARGIND2{print b-$0} a.txt b.txt
a-a
a-b
a-c
b-c
b-d
b-eENVIRON ENVIRON调用系统变量 [rootopenEulter-1 ~]# awk BEGIN{print ENVIRON[HOME]}
/root
## 如果是设置的环境变量还需要用export导入到系统变量中才能调用
[rootopenEulter-1 ~]# awk BEGIN{print ENVIRON[a]}[rootopenEulter-1 ~]# export a123
[rootopenEulter-1 ~]# awk BEGIN{print ENVIRON[a]}
123FILENAME FILENAME是当前处理文件的文件名 [rootopenEulter-1 ~]# awk FNRNR{print FILENAME-$0}FNR!NR{print FILENAME-$0} a.txt b.txt
a.txt-a
a.txt-b
a.txt-c
b.txt-c
b.txt-d
b.txt-eIGNORECASE IGNORECASE1代表忽略大小写 [rootopenEulter-1 ~]# echo A a b c|xargs -n1|awk BEGIN{IGNORECASE1}/a/
A
a操作符 运算符描述(…)分组$字段引用 –递增和递减 - !加号将字符串转化为数字如果字符串以数字开头则提取数字部分并输出否则输出0减号将字符串转化为数字如果字符串以数字开头则提取数字部分并取反输出否则输出0逻辑否定| |乘、除、取余 ! 关系运算符~ !~正则表达式匹配否定正则表达式匹配in数组成员 ||逻辑and、逻辑or?:简写条件表达式expr1 ? expr2 : expr3第一个表达式为真执行 expr2否则执行 expr3 - * / % ^变量赋值运算符 注意 在 awk 中有 3 种情况表达式为假数字是 0空字符串和未定义的值。 数值运算未定义变量初始值为 0。字符运算未定义变量初始值为空。 [rootopenEulter-1 ~]# awk BEGIN{s;if(s)print true;else printfalse}
false
[rootopenEulter-1 ~]# awk BEGIN{n0;if(n)print true;else print false}
false
[rootopenEulter-1 ~]# awk BEGIN{if(n)print true;else print false}
false 示例 截取整数 [rootopenEulter-1 ~]# echo 123abc abc123 123abc123|xargs -n1 | awk {print $0}
123
0
123
[rootopenEulter-1 ~]# echo 123abc abc123 123abc123|xargs -n1 | awk {print -$0}
-123
0
-123 感叹号 # 打印奇数行
[rootopenEulter-1 ~]# seq 6| awk i!i
1
3
5## 解释当处理第一行输入时执行 i!i。此时 i 初始值为 0false!i 则为 1true并将 1 赋值给 i。由于 i 的值为 1true在 awk 中条件为真时会默认执行 print 操作所以会输出当前行即 1。
当处理第二行输入时再次执行 i!i。此时 i 的值为 1true!i 为 0false并将 0 赋值给 i。因为 i 的值为 0false所以不会输出当前行。
当处理第三行输入时又执行 i!i。此时 i 的值为 0false!i 为 1true并将 1 赋值给 i。由于 i 的值为 1true所以会输出当前行即 3。
以此类推对于后续的每一行输入i 的值会在 0 和 1 之间交替变化导致奇数行对应的 i 值为 1true而被输出偶数行对应的 i 值为 0false而不被输出# 打印偶数行
[rootopenEulter-1 ~]# seq 6| awk !(i!i)
2
4
6 不匹配某行 [rootopenEulter-1 ~]# tail /etc/services | awk !/axio/{print $0}
aigairserver 21221/tcp # Services for Air Server
ka-kdp 31016/udp # Kollective Agent Kollective Delivery
ka-sddp 31016/tcp # Kollective Agent Secure Distributed Delivery
edi_service 34567/udp # dhanalakshmi.org EDI Service
pmwebapi 44323/tcp # Performance Co-Pilot client HTTP API
cloudcheck-ping 45514/udp # ASSIA CloudCheck WiFi Management keepalive
cloudcheck 45514/tcp # ASSIA CloudCheck WiFi Management System
spremotetablet 46998/tcp # Capture handwritten signatures 乘法和除法及取模 [rootopenEulter-1 ~]# seq 5 | awk {print $0*2}
2
4
6
8
10
[rootopenEulter-1 ~]# seq 5 | awk {print $0/2}
0.5
1
1.5
2
2.5
[rootopenEulter-1 ~]# seq 5 | awk $0%20{print $0}
2
4
[rootopenEulter-1 ~]# seq 5 | awk $0%2!0{print $0}
1
3
5管道符的使用 [rootopenEulter-1 ~]# seq 5 | shuf | awk {print $0|sort}
1
2
3
4
5
## shuf 命令用于对输入的行进行随机排序
## print $0|sort这是一个管道操作。print $0 会将当前行的内容输出| 是管道符号它将 print 的输出传递给 sort 命令。sort 命令用于对输入的行进行排序默认是按字典序升序排列。正则表达式匹配 [rootopenEulter-1 ~]# seq 5 | awk $0~3{print $0}
3
[rootopenEulter-1 ~]# seq 5 | awk $0!~3{print $0}
1
2
4
5
[rootopenEulter-1 ~]# seq 5 | awk $0~/[34]/{print $0}
3
4
[rootopenEulter-1 ~]# seq 5 | awk $0!~/[34]/{print $0}
1
2
5
[rootopenEulter-1 ~]# seq 5 | awk $0~/[^34]/{print $0}
1
2
5判断数组成员 [rootopenEulter-1 ~]# awk BEGIN{a[a]123}END{if(a in a)printyes}/dev/null
yes 三目运算符 [rootopenEulter-1 ~]# awk BEGIN{print 11?yes:no}
yes
[rootopenEulter-1 ~]# seq 5 | awk {print n(n?n,$0:$0)}
1
1,2
1,2,3
1,2,3,4
1,2,3,4,5
## 解释n?n,$0:$0这是一个三元运算符表达式其语法为 条件?表达式1:表达式2。如果条件为真则返回表达式 1 的值如果条件为假则返回表达式 2 的值。
当处理第一行输入时n 初始值为空字符串在布尔语境下相当于 false。所以 n?n,$0:$0 会返回 $0即当前行的内容。此时 n 被赋值为当前行内容 1并通过 print 输出 1。
当处理第二行输入时n 已经被赋值为 1在布尔语境下为 true。所以 n?n,$0:$0 会返回 n,$0也就是将 n 的值即 1和当前行内容 2 用逗号连接起来得到 1,2。然后将 1,2 赋值给 n 并输出。
当处理第三行输入时n 的值为 1,2为 true。n?n,$0:$0 返回 n即 1,2和当前行内容 3 用逗号连接的结果 1,2,3再将其赋值给 n 并输出。
以此类推对于后续的每一行输入都会将之前积累的内容 n 和当前行内容用逗号连接起来更新 n 的值并输出。# 每三行后面添加新一行
[rootopenEulter-1 ~]# seq 10 | awk {print NR%3?$0:$0\ntxt}
1
2
3
txt
4
5
6
txt
7
8
9
txt
10
# 两行合并一行
[rootopenEulter-1 ~]# seq 6|awk {printf NR%2!0?$0 :$0 \n}
1 2
3 4
5 6
[rootopenEulter-1 ~]# seq 6|awk ORSNR%2? :\n
1 2
3 4
5 6
## 解释
ORSORS 是 awk 的内置变量代表输出记录分隔符Output Record Separator默认值是换行符 \n。awk 在输出每一行内容后会自动添加 ORS 所代表的分隔符。
NRNR 也是 awk 的内置变量它表示当前处理的行号从 1 开始计数。
NR%2使用取模运算符 % 计算当前行号除以 2 的余数。如果余数为 1说明当前行号是奇数如果余数为 0说明当前行号是偶数。
NR%2? :\n这是一个三元运算符表达式语法为 条件?表达式1:表达式2。如果 NR%2 的结果为真即余数为 1当前行号是奇数则返回表达式 1 的值即空格 如果 NR%2 的结果为假即余数为 0当前行号是偶数则返回表达式 2 的值即换行符 \n。
ORSNR%2? :\n将三元运算符的结果赋值给 ORS。这意味着当处理奇数行时ORS 被设置为空格 当处理偶数行时ORS 被设置为换行符 \n
[rootopenEulter-1 ~]# seq 6 | awk {if(NR%2)ORS ;else ORS\n;print}
1 2
3 4
5 6 变量赋值 # 字段求和
## sum未初始化默认值为0
[rootopenEulter-1 ~]# seq 5 | awk {sum1}END{print sum}
5
[rootopenEulter-1 ~]# seq 5 | awk {sum$0}END{print sum}
15 [rootopenEulter-1 ~]# awk ‘BEGIN{print 11?“yes”:“no”}’ yes [rootopenEulter-1 ~]# seq 5 | awk ‘{print n(n?n,$0:$0)}’ 1 1,2 1,2,3 1,2,3,4 1,2,3,4,5 解释n?n,$0:$0这是一个三元运算符表达式其语法为 条件?表达式1:表达式2。如果条件为真则返回表达式 1 的值如果条件为假则返回表达式 2 的值。 当处理第一行输入时n 初始值为空字符串在布尔语境下相当于 false。所以 n?n,“$0:$0 会返回 $0即当前行的内容。此时 n 被赋值为当前行内容 1并通过 print 输出 1。 当处理第二行输入时n 已经被赋值为 1在布尔语境下为 true。所以 n?n”,“$0:$0 会返回 n”,“$0也就是将 n 的值即 1和当前行内容 2 用逗号连接起来得到 1,2。然后将 1,2 赋值给 n 并输出。 当处理第三行输入时n 的值为 1,2为 true。n?n”,$0:$0 返回 n即 1,2和当前行内容 3 用逗号连接的结果 1,2,3再将其赋值给 n 并输出。 以此类推对于后续的每一行输入都会将之前积累的内容 n 和当前行内容用逗号连接起来更新 n 的值并输出。 每三行后面添加新一行 [rootopenEulter-1 ~]# seq 10 | awk ‘{print NR%3?$0:$0\ntxt}’ 1 2 3 txt 4 5 6 txt 7 8 9 txt 10 两行合并一行 [rootopenEulter-1 ~]# seq 6|awk ‘{printf NR%2!0?$0 “:$0” \n}’ 1 2 3 4 5 6 [rootopenEulter-1 ~]# seq 6|awk ‘ORSNR%2? “:”\n’ 1 2 3 4 5 6 解释 ORSORS 是 awk 的内置变量代表输出记录分隔符Output Record Separator默认值是换行符 \n。awk 在输出每一行内容后会自动添加 ORS 所代表的分隔符。 NRNR 也是 awk 的内置变量它表示当前处理的行号从 1 开始计数。 NR%2使用取模运算符 % 计算当前行号除以 2 的余数。如果余数为 1说明当前行号是奇数如果余数为 0说明当前行号是偶数。 NR%2? “:”\n这是一个三元运算符表达式语法为 条件?表达式1:表达式2。如果 NR%2 的结果为真即余数为 1当前行号是奇数则返回表达式 1 的值即空格 “如果 NR%2 的结果为假即余数为 0当前行号是偶数则返回表达式 2 的值即换行符 “\n”。 ORSNR%2?” “:”\n将三元运算符的结果赋值给 ORS。这意味着当处理奇数行时ORS 被设置为空格 “当处理偶数行时ORS 被设置为换行符 “\n” [rootopenEulter-1 ~]# seq 6 | awk {if(NR%2)ORS” “;else ORS”\n;print}’ 1 2 3 4 5 6 变量赋值 # 字段求和
## sum未初始化默认值为0
[rootopenEulter-1 ~]# seq 5 | awk {sum1}END{print sum}
5
[rootopenEulter-1 ~]# seq 5 | awk {sum$0}END{print sum}
15