1

Can someone suggest how I can get the value 45 after parsing an example json text as shown below :

....
"test": 12
"job": 45
"task": 11
.....

Please note that I am aware of tools like jq and others but this requires it to be installed.

I am hoping to get this executed using grep, awk or sed command.

4
  • 3
    Installing jq is not that hard: it's a single binary file (no external dependencies) that can be copied into any directory. Commented Aug 10, 2016 at 16:44
  • Why not use a specialized tool like jq for the job, jq is sed forjson Commented Aug 10, 2016 at 16:57
  • Also that doesn't look like valid json to me :/ Commented Aug 10, 2016 at 18:26
  • Is Python, PHP or Perl installed on the server? Commented Aug 10, 2016 at 22:39

4 Answers 4

2
awk -F'[[:space:]]*:[[:space:]]*' '/^[[:space:]]*"job"/{ print $2 }'
sed -n 's/^[[:space:]]*"job"[[:space:]]*:[[:space:]]*//p'
Sign up to request clarification or add additional context in comments.

4 Comments

It's very unlikely that a key in a json file starts at the begin of the line. While a simple task can indeed be done with grep/sed/awk, I would strongly recommend jq. It's not hard to install it - like chepner mentioned
The OP already stated he can't/won't install jq and asked for a solution using standard UNIX tools instead. I use plenty of machines where I don't have permission to install any tools, maybe that's the case for the OP too, idk. I also use lots of machines I have to jump on and do something with quickly so I tend to avoid solutions that use non-standard tools for that reason too. I'll add some optional white space at the front in case the OPs real input doesn't follow the format of the input he posted.
Sure, for a short interactive hack that might be good enough. But for a script or program not. If the OP can't use jq, he can use a scripting language like PHP, Python, Perl or whatever. Something that can process json is almost certainly installed.
If jq WAS installed then he'd probably have php, pyhon, perl, etc. but if it's not then there's no reason to think any of those others non-standard tools are and if the OP can't install jq then he almost certainly can't install any of the others.
2

You can use grep -oP (PCRE):

grep -oP '"job"\s*:\s*\K\d+' file

45

\K is used for reseting the previously matched data.

Comments

0

Using awk, if you just want to print it:

awk -F ':[ \t]*' '/^.*"job"/ {print $2}' filename

Above command matches any line that has "job" at the beginning of a line, and then prints the second column of that line. awk option -F is used to set the column separator as : followed by any number of spaces or tabs.

If you want to store this value in bash variable job_val:

job_val=$(awk -F ':[ \t]*' '/^.*"job"/ {print $2}' filename)

Comments

0

Use specialized tools like jq for the task :

Had your file looked like

[
{
"test": 12,
"job": 45,
"task": 11
}
]

below stuff would get you home

jq ".[].job" file

Had your file looked like

{
"stuff" :{
.
.
"test": 12,
"job": 45,
"task": 11
.
.
}
}

below

jq ".stuff.job" file

would get you home.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.