Only with the grep
you won’t get the string in the format you need (numero - url
). With the help of the tool awk
gets easier:
curl -s 'http://brasil.br/pub/retorno.json' | grep -Po '"(numero|url)": "\K[^"]*' | awk 'NR%2{ printf "%s - ", $0; next;}1'
Due to some limitations of the lookbehind, such as accepting only fixed-size expressions, I made a small modification to the regular expression. The \K
serves as a good alternative to the lookbehind, as it is more flexible and still allows us to ignore everything that is before it, since it makes only what is after it is considered in the result of the expression.
The awk
is a very useful tool for filtering texts. We are using awk
for every two lines, they are printed in the format linha_atual - linha_seguinte
.
Have you looked at this tool: https://stedolan.github.io/jq/ ?
– PerryWerneck