gpt4 book ai didi

regex - 在 awk 中使用匹配模式

转载 作者:行者123 更新时间:2023-12-02 07:19:54 24 4
gpt4 key购买 nike

我想使用 awk 打印匹配的模式。不是场,不是线。

在 vi 中,您可以将匹配的模式放在替换中,方法是用圆括号括起来并用花括号和数字引用它,如下所示:

:s/bufid=([0-9]*)/buffer id is {\0}/

parens 之间匹配的部分被记住并可以使用。

在perl中,类似

$_ = "Hello there, neighbor";
if (/\s(\w+),/) { # memorize the word between space and comma
print "the word was $1\n"; # the word was there
}

有什么方法可以用 awk 做类似的事情吗?我只想提取缓冲区 ID 并打印它,而且只有它。

输入行是 XML,并且将包含(除其他外)'bufId="123456"'。我要打印“123456”

所以...

awk < file.xml '/bufId="([0-9]*)"/ { print X; }'

我在 X 所在的位置放什么?

这能做到吗?

最佳答案

使用 gawk

awk '{print gensub(/.*bufId="([0-9]*)"/,"\\1",1)}'

如果您想要引用结果,您还必须捕获引号。

关于regex - 在 awk 中使用匹配模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49185024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com