【问题标题】:AWK print only the lines which have dotAWK 只打印有点的行
【发布时间】:2021-02-23 02:55:13
【问题描述】:

如何仅打印第 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).

【问题讨论】:

  • 如果您edit 您的问题包含minimal reproducible example 并具有简洁、可测试的样本输入和预期输出,那么我们可以为您提供最好的帮助。见How to Ask
  • 总是试着用积极而不是消极的方式来思考和表达你的要求,例如。而不是remove the line which do not have dot,你可以说print only the lines which have dot。这使得需求更加清晰,避免双重否定,并避免你的需求中的否定渗透到你的代码中并使代码变得复杂。
  • @EdMorton,非常感谢您的投入,我总是遵循您的好技巧,所以我刚刚编辑了我的问题,我试图从语法上重新表述我的帖子,但我没有作为非英语母语的人,英语流利。
  • 不客气。不过,我并没有要求提供原始数据,而是要求minimal reproducible example 提供简洁、可测试的样本输入和预期输出 - 您提供的原始数据并不是展示您的要求所需的最小输入集,而是预期的鉴于输入仍然缺失,因此您提供的不是minimal reproducible example 的输出。

标签: shell awk


【解决方案1】:

根据您显示的尝试,请尝试以下操作,因为没有提供样品,因此无法测试。

awk '{a[$5]++} END{for(i in a){if(i~/\./){print a[i]"  "i}}}' Input_file

解释:基本上在您尝试的程序的END 块中添加一个额外的检查,以检查变量 i 是否包含点,然后只打印它们,否则不打印。 p>

要处理冒号(根据所示尝试在第一个冒号之前获取值)也可以尝试以下操作:我也可以对变量 i 使用替换,但如果您可能需要进一步使用它,所以我在这里使用 split .用: 分隔符拆分它的值并打印它的第一个值。这也没有考虑到冒号总是在最后。

awk '{a[$5]++} END{for(i in a){if(i~/\./){split(i,arr,":");print a[i]"  "arr[1]}}}' Input_file

sub:根据 OP 的 cmets 也在此处添加此变体,考虑到您的冒号将始终位于值的最后。

awk '{a[$5]++} END{for(i in a){if(i~/\./){sub(/:$/,"",i);print a[i]"  "i}}}' Input_file

【讨论】:

  • 非常感谢,这行得通,但我们可以添加删除: 吗?
  • @kulfi,欢迎您,请尝试我编辑的代码,如果对您有帮助,请告诉我?
  • 再次非常感谢您,它工作得很好,但是什么是`我也可以在变量上使用替换`,如果可以展示这一点,那么可能对未来的访问者也有好处。
  • @kulfi,当然我现在也添加了该解决方案,祝您学习愉快。
【解决方案2】:

这应该可以...

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

【讨论】:

  • 它将保持报告的计数相同,因为没有重新调整它应该是相同的。
  • 啊,你是对的,傻我,我想多了++
  • 感谢 Karakfa,很好,但是我们多次调用 $5,但很优雅。
  • @kulfi,没有“调用”,它是一个字段引用,但是是的,多次。请参阅替代方案。
【解决方案3】:

如何删除不包含点(.)的行。
其次,从最后看到的每一行中删除冒号(:)。

你可以试试这个awk:

awk -v OFS='\t' '
{
   sub(/:$/, "", $5)
   a[$5]++
}
END {
   for (i in a)
      if (index(i, "."))
         print a[i], i
}' netappev.logs

【讨论】:

  • 谢谢Anubhava,你能解释一下(index(i, ".")吗?
  • index 是执行字符串搜索的功能(非正则表达式和更快)。这里它在变量i 中搜索. 的第一个位置。如果没有找到它返回位置或0
  • 啊,谢谢。我知道这一点,但打错了
【解决方案4】:

从您发布的数据块看来,这可能是您正在尝试做的事情,但没有相关的预期输出,这是一个猜测:

$ 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

【讨论】:

  • 感谢 Ed Morton 抽出时间提供您的答案 +1,一如既往的棒。
猜你喜欢
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 2011-01-02
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多