If you're parsing structured text like JSON, use a dedicated tool that understands the structure, as generic text processing tools will depend on cues like newlines and whitespace which are not essential to the structure.
So, using jq, you could get values of all .OutputResults keys using something like:
jq '.. | select(.OutputResults?) | .OutputResults'
For example:
% cat foo.json | jq '.. | select(.OutputResults?) | .OutputResults'
{
"first": 0,
"second": 2
}
{
"first": 0,
"second": 2
}
Or if you need the .OutputResults as part of the output:
% jq '.. | select(.OutputResults?) | {OutputResults}' foo.json
{
"OutputResults": {
"first": 0,
"second": 2
}
}
{
"OutputResults": {
"first": 0,
"second": 2
}
}
Or with compact output:
% jq '.. | select(.OutputResults?) | {OutputResults}' -c < foo.json
{"OutputResults":{"first":0,"second":2}}
{"OutputResults":{"first":0,"second":2}}
For reading very large JSON files with jq, we have to use its "streaming mode", but the way jq does stream makes it much more complicated to use. I think the following jq program, obtained by tweaking the example in jq FAQ, should work for showing just the values of of OutputResults keys:
foreach inputs as $in (
null;
if has("OutputResults") then null
else . as $x
| $in
| if length != 2 and $x then {"OutputResults": $x}
elif length != 2 then null
elif .[0][-2] == "OutputResults" then ($x + {"\(.[0][-1])": "\(.[-1])"})
else $x
end
end;
select(has("OutputResults")) | .
)
Put this in a file, say, outputresults.jq, and use it like so:
jq -n --stream -f outputresults.jq some-inputjson