awk 是否能够像 perl 一样提取匹配的分组?
echo "file:/home/todd/a.txt, size:123, md5sum:112sdfddddd, status:success" \
| awk '/file:(.*), size:(.*),/ { print $1 $2}'
实际上,我想提取“/home/todd/a.txt”和“123”,但 $1 和 $2 不适合它。有没有像 perl 那样提取匹配分组的方法?
echo "file:/home/todd/a.txt, size:123, md5sum:112sdfddddd, status:success" \
| awk '/file:(.*), size:(.*),/ { print $1 $2}'
Actually, I want to extract "/home/todd/a.txt" and "123", but the $1 and $2 is not for it. Is there method for extracting matched grouping as perl?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 gawk 中使用 gensub 来支持反向引用,但是,通常,您可以执行以下
逻辑:以逗号分割,遍历每个字段,检查文件或大小,然后以“:”分割以获得第二个元素。
you can use gensub in gawk for backreferences support, however, generally, you can just do this
Logic: Split on commas, go through each field, check for file or size, then split on ":" to get the 2nd element.
gawk
具有gensub()
函数,该函数具有您引用的反向引用。但是,如果您不想使用perl
以外的其他东西,我建议使用sed
来实现此目的,awk
在您考虑事情时效果最好就领域而言。在您的情况下,您可以使用冒号:
和逗号,
作为字段分隔符来删除文本,如下所示:概念证明
gawk
has thegensub()
function which has the back references you refer to. However, I would suggest usingsed
for this if you don't want to use something other thanperl
awk
works best when you think of things in terms of fields. In your case you could use both the colon:
and comma,
as field separators to strip out your text like so:Proof of Concept