grep中的仅整数通配符

时间:2019-07-08 22:09:04

标签: bash macos grep wildcard

我有一条命令可以输出如下所示的收集字符串:

json.formats[0]].url = "https://example.com/ar.html"
json.formats[1].url = "https://example.com/es.html"
json.formats[2s].url = "https://example.com/ru.html"
json.formats[3].url = "https://example.com/pt.html"
json.formats[73].url = "https://example.com/ko.html"
json.formats[1502].url = "https://example.com/pl.html"

(还有更多实例,但是出于简单起见,它们已被删除)

我可以使用下面的命令

myCmd | grep -e 'json\.formats\[.*\]\.url\ \=\ '

但是我只希望通配符匹配整数,并抛出非整数匹配。它给了我以下内容:

json.formats[0]].url = "https://example.com/ar.html"
json.formats[1].url = "https://example.com/es.html"
json.formats[2s].url = "https://example.com/ru.html"
json.formats[3].url = "https://example.com/pt.html"
json.formats[73].url = "https://example.com/ko.html"
json.formats[1502].url = "https://example.com/pl.html"

我真正想要的是:

json.formats[1].url = "https://example.com/es.html"
json.formats[3].url = "https://example.com/pt.html"
json.formats[73].url = "https://example.com/ko.html"
json.formats[1502].url = "https://example.com/pl.html"

谢谢:-)

1 个答案:

答案 0 :(得分:4)

您可以使用:

myCmd | grep -E 'json\.formats\[[[:digit:]]+\]\.url = '

或:

myCmd | grep -E 'json\.formats\[[0-9]+\]\.url = '

[[:digit:]]相当于大多数语言环境的[0-9]