целочисленные подстановочные знаки в grep - PullRequest
1 голос
/ 09 июля 2019

У меня есть команда, которая выводит строки коллекции, которые выглядят так:

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 Ответ

4 голосов
/ 09 июля 2019

Вы можете использовать:

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

или:

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

[[:digit:]] эквивалентно [0-9] для большинства языков.

...