如何仅打印第5列中包含点(.
)的行,并删除末尾的冒号(:
)
我想避免对|
重复使用awk,而不是喜欢用单个awk
做所有的技巧
这是我使用awk
的命令输出
$ awk '{a[$5]++;} END{for(i in a) print a[i]" "i}' netappev.logs
4 wafl.vol.full:
5
3 sis.changelog.full:
1 17:58:41
1 using
1 "event
21 vifmgr.bcastDomainPartition:
4 wafl.mirror.access.denied:
76 secd.authsys.lookup.failed:
7 wafl.vol.runningOutOfInodes:
用grep
打印带有点(.
)的行:
$ awk '{a[$5]++;} END{for(i in a) print a[i]" "i}' netappev.logs | grep \.
4 wafl.vol.full:
3 sis.changelog.full:
21 vifmgr.bcastDomainPartition:
4 wafl.mirror.access.denied:
76 secd.authsys.lookup.failed:
7 wafl.vol.runningOutOfInodes:
删除冒号(:
)使用|
与AWK
。
$ awk '{a[$5]++;} END{for(i in a) print a[i]" "i}' netappev.logs | grep \. | awk -F: '{print $1}'
4 wafl.vol.full
3 sis.changelog.full
21 vifmgr.bcastDomainPartition
4 wafl.mirror.access.denied
76 secd.authsys.lookup.failed
7 wafl.vol.runningOutOfInodes
编辑:
原始数据,由@Ed Morton在评论中询问。
Last login time: 2/19/2021 17:58:41
Time Node Severity Event
------------------- ---------------- ------------- ---------------------------
2/19/2021 22:32:16 ksg6007 ERROR vifmgr.bcastDomainPartition: Broadcast domain KSGI_mgmt-38 is partitioned into 2 groups on node ksg6007. The different groups are: {a0a-38}, {e0M}. LIFs hosted on the ports in this broadcast domain may be at the risk of seeing connectivity issues.
2/19/2021 22:31:31 ksg6013 ERROR wafl.vol.runningOutOfInodes: The file system on Volume tpv6010_ida_ipdev_workareas@vserver:577ffe3f-4d04-11e7-92cf-00a098c5e37c is using 80 percent or more of the files that can be contained on the volume.
2/19/2021 22:30:03 ksg6005 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (28274) on Vserver (tpv6009) for client with IP address (10.137.170.142).
2/19/2021 22:29:52 ksg6007 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (45509) on Vserver (tpv6011) for client with IP address (10.137.170.112).
2/19/2021 22:29:52 ksg6014 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (45509) on Vserver (tpv6011) for client with IP address (10.137.170.112).
2/19/2021 22:28:37 ksg6006 ERROR vifmgr.bcastDomainPartition: Broadcast domain KSGI_mgmt-38 is partitioned into 2 groups on node ksg6006. The different groups are: {a0a-38}, {e0M}. LIFs hosted on the ports in this broadcast domain may be at the risk of seeing connectivity issues.
2/19/2021 22:28:01 ksg6008 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (28274) on Vserver (tpv6009) for client with IP address (10.137.170.142).
2/19/2021 22:27:15 ksg6014 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (46537) on Vserver (tpv6010) for client with IP address (10.137.171.26).
2/19/2021 22:27:00 ksg6005 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (35828) on Vserver (tpvtp605) for client with IP address (165.114.117.122).
2/19/2021 22:25:49 ksg6013 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (997) on Vserver (tpv6010) for client with IP address (165.114.118.231).
2/19/2021 22:24:49 ksg6005 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (45509) on Vserver (tpv6010) for client with IP address (10.137.170.112).
2/19/2021 22:24:49 ksg6007 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (45509) on Vserver (tpv6010) for client with IP address (10.137.170.112).
2/19/2021 22:24:49 ksg6008 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (45509) on Vserver (tpv6011) for client with IP address (10.137.170.112).
2/19/2021 22:23:49 ksg6008 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (46537) on Vserver (tpv6010) for client with IP address (10.137.171.26).
2/19/2021 22:23:49 ksg6013 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (46537) on Vserver (tpv6010) for client with IP address (10.137.171.26).
2/19/2021 22:23:47 ksg6005 ERROR vifmgr.bcastDomainPartition: Broadcast domain KSGI_mgmt-38 is partitioned into 2 groups on node ksg6005. The different groups are: {a0a-38}, {e0M}. LIFs hosted on the ports in this broadcast domain may be at the risk of seeing connectivity issues.
2/19/2021 22:23:35 ksg6008 ERROR vifmgr.bcastDomainPartition: Broadcast domain KSGI_mgmt-38 is partitioned into 2 groups on node ksg6008. The different groups are: {a0a-38}, {e0M}. LIFs hosted on the ports in this broadcast domain may be at the risk of seeing connectivity issues.
2/19/2021 22:22:37 ksg6007 ALERT wafl.vol.full: Insufficient space on volume tpv6009_scratch001@vserver:41dd0809-477f-11e7-92cf-00a098c5e37c to perform operation. 8.00KB was requested but only 1.00KB was available.
2/19/2021 22:22:35 ksg6013 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (46329) on Vserver (tpv6009) for client with IP address (165.114.116.50).
2/19/2021 22:21:12 ksg6006 ERROR wafl.mirror.access.denied: Access to SnapMirror volume 'tpv6010_da' was denied because SnapMirror initialization was not complete (exported Snapshot copy identifier '0' is invalid).
2/19/2021 22:21:04 ksg6014 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (46537) on Vserver (tpv6009) for client with IP address (165.114.118.133).
2/19/2021 22:21:02 ksg6005 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (46709) on Vserver (tpv6009) for client with IP address (10.137.169.63).
2/19/2021 22:21:02 ksg6005 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (46329) on Vserver (tpv6011) for client with IP address (10.137.170.238).
2/19/2021 22:20:58 ksg6007 ERROR vifmgr.bcastDomainPartition: Broadcast domain KSGI_mgmt-38 is partitioned into 2 groups on node ksg6007. The different groups are: {a0a-38}, {e0M}. LIFs hosted on the ports in this broadcast domain may be at the risk of seeing connectivity issues.
2/19/2021 22:20:02 ksg6005 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (46329) on Vserver (tpv6010) for client with IP address (10.137.170.238).
2/19/2021 22:19:43 ksg6013 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (46709) on Vserver (tpv6011) for client with IP address (10.137.169.63).
2/19/2021 22:19:08 ksg6013 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (994) on Vserver (tpv6011) for client with IP address (165.114.116.101).
2/19/2021 22:19:02 ksg6008 ERROR secd.authsys.lookup.failed: Unable to retrieve credentials for UNIX user with UID (45509) on Vserver (tpv6009) for client with IP address (10.137.170.112).
由于没有提供样品,因此无法测试,请尝试以下操作。
awk '{a[$5]++} END{for(i in a){if(i~/./){print a[i]" "i}}}' Input_file
解释:基本上是在你尝试的程序的END
块中添加一个额外的检查,检查变量i是否包含点,然后只打印它们,否则不打印。
要处理冒号(按所示尝试在第1个冒号之前获取值)也尝试以下操作:我也可以在变量I上使用替换,但如果您可能需要进一步使用它,所以我在这里使用split
。用:
分隔符拆分它的值并打印它的第一个值。这也没有考虑冒号总是出现在最后。
awk '{a[$5]++} END{for(i in a){if(i~/./){split(i,arr,":");print a[i]" "arr[1]}}}' Input_file
或sub
:根据OP的评论,在这里添加它也是这个变体,考虑到您的冒号总是出现在值的最后。
awk '{a[$5]++} END{for(i in a){if(i~/./){sub(/:$/,"",i);print a[i]" "i}}}' Input_file
这应该是…
awk '$5~/./ {sub(/:$/,"",$5); a[$5]++}
END {for(i in a) print a[i] " " i}' file
在计数之后删除:
可能会产生不想要的结果,假设您有text:和text,它们将显示为两个单独的行。
如果您不喜欢多次引用同一个字段,请在第一次使用时为变量赋值。
awk '(k=$5)~/./ {sub(/:$/,"",k); a[k]++}
END {for(k in a) print a[k] " " k}' file
如何删除不包含点(
.
)的行。
其次,从每行删除冒号(:
),就像我们在末尾看到的那样。
你可以试试这个awk
:
awk -v OFS='t' '
{
sub(/:$/, "", $5)
a[$5]++
}
END {
for (i in a)
if (index(i, "."))
print a[i], i
}' netappev.logs
从你发布的数据块来看,这可能是你想要做的,但没有相关的预期输出,这是一个猜测:
$ awk -F'[: ]+' 'NF>7{print $7}' file | sort | uniq -c
20 secd.authsys.lookup.failed
5 vifmgr.bcastDomainPartition
1 wafl.mirror.access.denied
1 wafl.vol.full
1 wafl.vol.runningOutOfInodes
或者如果您喜欢awk:
$ awk -F'[: ]+' 'NF>7{cnt[$7]++} END{for (i in cnt) print cnt[i], i}' file
1 wafl.vol.full
5 vifmgr.bcastDomainPartition
20 secd.authsys.lookup.failed
1 wafl.mirror.access.denied
1 wafl.vol.runningOutOfInodes