2

I wish to extracted out elapsedTime attribute values from the file.

Records look

{"realm":"/test","transactionId":"9e26c614","elapsedTime":17,"elapsedTimeUnits":"MILLISECONDS","_id":"9e26c6asdasd"}

The file I am having is in gb's and I want to get the values greater than 10000.

I tried to grep but due to colon grep is not working.

grep -wo --color 'elapsedTime' fileName -> this just prints attribute names
grep -w  --color "elapsedTime" fileName -> this just highlights the attribute. 
0

2 Answers 2

8

The data is JSON format so it's best to use a parser that understands this format.

This will pick out the elapsedTime value from the JSON in the file /tmp/data

jq .elapsedTime /tmp/data
17

This will pick out only those values larger than 10000

jq '.elapsedTime | select(. > 10000)' /tmp/data

If you really cannot use jq then a sed|awk construct can be considered. However, this requires that there must be only one elapsedTime label and associated value per line. There may be other caveats and I really do not recommend it, but if you're desperate here it is,

sed -En 's/^.*"elapsedTime":\s*"?([0-9]+).*$/\1/p' /tmp/data |
    awk '$1 > 10000'

In response to a follow-up question (comment), to pick out two elements you need to filter on a single element from the object, and then display the required elements:

jq -r 'select (.elapsedTime > 10000) | [ .elapsedTime, .transactionId ] | @tsv ' /tmp/data
0
1

If jq not available, sed can be used.

ELAP=$(sed 's/.*"elapsedTime":\([0-9]*\).*/\1/' filename)

Try it online!

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.