awk用法

图片[1]-awk用法-晴天生活分享日志

1.插入新字段和格式化空白
[root@iZbp1cr1sgiqi6r6qlygx9Z ~]# cat kkkkk.txt
aaaa bbb ccc
bbb aaa ccc
ddd fff eee gg hh ii jj

[root@iZbp1cr1sgiqi6r6qlygx9Z ~]# awk ‘{$1=$1; print}’ kkkkk.txt
aaaa bbb ccc
bbb aaa ccc
ddd fff eee gg hh ii jj
[root@iZbp1cr1sgiqi6r6qlygx9Z ~]# awk ‘BEGIN{OFS=”\t”}{$1=$1;print}’ kkkkk.txt
aaaa bbb ccc
bbb aaa ccc
ddd fff eee gg hh ii jj

2.从ifconfig中筛选ipv4地址

[root@iZbp1cr1sgiqi6r6qlygx9Z ~]# ifconfig eth0
eth0: flags=4163 mtu 1500
inet 172.17.158.245 netmask 255.255.240.0 broadcast 172.17.159.255
inet6 fe80::216:3eff:fe32:281b prefixlen 64 scopeid 0x20
ether 00:16:3e:32:28:1b txqueuelen 1000 (Ethernet)
RX packets 1222033 bytes 1053753987 (1004.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 654425 bytes 97468302 (92.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

方法一:ifconfig eth0 | awk ‘NR==2{ print $2}’
方法二:ifconfig eth0 | awk ‘/inet/ && !($2 ~ /^fe80/){print $2}’ 正则匹配
方法三:ifconfig eth0 | awk ‘/^.*172/{ print $2}’
方法四:ifconfig eth0 | awk ‘BEGIN{RS= ” “}NR==1{print $6}’ 段落读取
或 ifconfig eth0 | awk ‘BEGIN(RS=” “)’!/^lo:/{print $6}
RS=”” 设置空字符串,按段落读取
“\0”一次性读取所有数据 例外\0
“^S” 真正一次性读取所有数据
“\n+”按行读取但忽略所有空行

方法5:ifconfig | awk ‘BEGIN{RS=” “;FS=”\n”}!/^/lo:/{FS=\n”;$0=$2;$0=$0;print $2’

  1. 统计access.log访问ip次数并排序
    awk ‘{ip[$1]++} END{for (i in ip) print i, ip[i]}’ /www/wwwlogs/access.log
    awk ‘{ip[$1]++} END{for (i in ip) print i, ip[i]}’ /www/wwwlogs/access.log | sort -k2nr | head -n10

4.统计tcp连接状态数量
netstat -tnap | awk ‘{tcp[$6]++}END{for(i in tcp){print i; tcp[i]} }’

------本页内容已结束,喜欢请分享------

感谢您的来访,获取更多精彩文章请收藏本站。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容