I am using grep
command to get the number of elements present in the json
API response (retrieved by curl
). Below is an example response:
[{"k1":"v1"},{"k2:"v2"}]
The above response contains 2 elements so I want the command to return 2. Below is what I have tried:
echo '[{"k1":"v1"},{"k2:"v2"}]' | grep -oP '\[{.+}\]' | wc -l
echo '[{"k1":"v1"},{"k2:"v2"}]' | grep -oP '{.+}' | wc -l
echo '[{"k1":"v1"},{"k2:"v2"}]' | grep -oP '\{.+\}' | wc -l
All these match the whole string and I just get 1
as an output for each of the above commands. What changes do I need to make in order for grep
to return the correct count? It needs to be key/value agnostic.