一些例子。
1、显示gopher帐号所在行至最后一行。
awk ‘/^gopher/{row=NR;while(getline < "passwd" ){++i;FS=":";if(i>=row){print $0}}}‘ passwd
getline < "filename" 若读取成功,则返回非0,当读文件完后,会返回0。
awk -F: ‘NR==FNR{if($1 ~ /^gopher/){row=NR};next}{if (FNR>=row){print $0}}‘ passwd passwd
用NR==FNR方法。
awk ‘/^gopher/{p=1}p‘ passwd
awk变量若是没有赋值,那么他的初始为0或空。另外1默认是1{print}
sed -n ‘/^gopher/,$p‘ passwd sed ‘/^gopher/,$ !d‘ passwd
2、提取网卡名称、mac地址、ip地址用一行显示
[[email protected] awk]# cat k eth0 Link encap:Ethernet HWaddr 00:50:56:8A:1E:24 inet addr:10.16.66.106 Bcast:10.16.66.255 Mask:255.255.255.0 inet6 addr: fe80::250:56ff:fe8a:1e24/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:19838444 errors:0 dropped:0 overruns:0 frame:0 TX packets:446941 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1630611345 (1.5 GiB) TX bytes:65236241 (62.2 MiB) Base address:0x2000 Memory:d1020000-d1040000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:7319 errors:0 dropped:0 overruns:0 frame:0 TX packets:7319 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:3808487 (3.6 MiB) TX bytes:3808487 (3.6 MiB) eth1 Link encap:Ethernet HWaddr 00:50:56:8A:1E:24 inet addr:172.16.66.106 Bcast:10.16.66.255 Mask:255.255.255.0 inet6 addr: fe80::250:56ff:fe8a:1e24/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:19838444 errors:0 dropped:0 overruns:0 frame:0 TX packets:446941 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1630611345 (1.5 GiB) TX bytes:65236241 (62.2 MiB) Base address:0x2000 Memory:d1020000-d1040000
[[email protected] awk]# sed -r -n ‘/^eth/{s/(eth[0-9]).*HWaddr (.*)/\1 \2/;h;n;s/.*inet addr:(.*)Bcast.*/\1/;H;x;s/\n//p}‘ k eth0 00:50:56:8A:1E:24 10.16.66.106 eth1 00:50:56:8A:1E:24 172.16.66.106 awk ‘/^eth/{x= $1" "$(NF);i=NR}{if (NR == i+1){x= x " "gensub(/.*addr:(.*)Bcast.*/,"\\1","g",$0);print x;x=""}}‘ k eth0 00:50:56:8A:1E:24 10.16.66.106 eth1 00:50:56:8A:1E:24 172.16.66.106
3、sub、gsub、match、gensub使用
sub使用:
sub(/正则/,"替换匹配到的字符串","要处理的字符串,默认是$0")
返回值:指替换正则的个数
sub的结果会修改原字符串,比如$0
只能替换第一个匹配到的值。
例:
[[email protected] awk]# cat a 1 a b 2 c d 3 huang xuewen 4 xx huang aa huang [[email protected] awk]# [[email protected] awk]# awk ‘{a=sub(/huang/,"ooo",$0);print a"==="$0}‘ a 0===1 a b 0===2 c d 1===3 ooo xuewen 1===4 xx ooo aa huang [[email protected] awk]#
gsub使用:
gsub与sub用法基本一样,sub只替换第一个匹配到的值,gsub替换所有匹配到的值。
[[email protected] awk]# cat a 1 a b 2 c d 3 huang xuewen 4 xx huang aa huang 5 xx huang aa huang xabc huang a [[email protected] awk]# awk ‘{a=gsub(/huang/,"ooo",$0);print a"==="$0}‘ a 0===1 a b 0===2 c d 1===3 ooo xuewen 2===4 xx ooo aa ooo 3===5 xx ooo aa ooo xabc ooo a [[email protected] awk]#
match使用:
match("原字符串","正则表达式",array)
返回值:若匹配到,则会返回匹配的位置,若是没有匹配到,则返回0
可以分组表示,若正则表达式中使用(引起的内容),那么会存放到array数据中。可以用array[1]表示第一个匹配到的分组内容。
[[email protected] awk]# cat k1 inet addr:10.16.66.106 Bcast:10.16.66.255 Mask:255.255.255.0 inet addr:172.16.66.168 Bcast:172.16.66.255 Mask:255.255.255.0 [[email protected] awk]# awk ‘{if(match($0,"[^:]*:([^ ]*).*Bcast:([^ ]*).*",a))print a[1],a[2] }‘ k1 10.16.66.106 10.16.66.255 172.16.66.168 172.16.66.255
gensub使用:
gensub("正则表达式","要替换表达式的字符串","1或g","处理的字符串")
1表示只替换一个,g替换所有
返回值
1、使用普通的值替换正则表达式,返回的是替换后的值。
2、若是使用\\1 这个表示返回的值是正则表达式中(引起的内容),\\2表示第二个
下面的正则乱写的。
[[email protected] awk]# cat k1 inet addr:10.16.66.106 Bcast:10.16.66.255 Mask:255.255.255.0 inet addr:172.16.66.168 Bcast:172.16.66.255 Mask:255.255.255.0 [[email protected] awk]# awk ‘{s=gensub("[^:]*:([^ ]*).*","hxw",1,$0);print s}‘ k1 hxw hxw [[email protected] awk]# awk ‘{s=gensub("[^:]*:([^ ]*).*","\\1",1,$0);print s}‘ k1 10.16.66.106 172.16.66.168
时间: 2024-10-13 19:57:45