I want to extract a value from a json file so that I can process it and I try that using grep '"USDEUR" currs.json' | cut -d ':' -f 2 but it returns 0.918695, and the json file looks like this:


So I want to know how to disable the comma so that I can process the value of USDEUR


5 Answers 5


Use a JSON parser to parse JSON, for example jq:

$ jq '.quotes.USDEUR' file.json 
  • That's probably the sanest way of all; using the right tool for the job.
    – dhag
    Dec 23, 2015 at 20:48

There are a number of ways to do it. You could pipe your results through any of the following

tr -d ','
cut -d',' -f1
sed -e 's/,//'

With pure grep using perl compatible regular expressions (-P option):

grep -Po 'USDEUR":\K.*(?=,)' file
  • \K will throw away everything till :
  • (?=,) will throw away everything after , (including ,)
  • One thing about this, if the USDEUR value is the last in the list, it won't have a comma. Perhaps 'USDEUR":\K.*(?=,|$)' Jan 19, 2022 at 16:52

As a general rule of thumb: if you pipe the output of a text tool (grep or sed) through cut you’re already doing something wrong. (Suggesting a second cut pipe is even more idiotic…)

sed -n '/"USDEUR"/s/^.*:\(.*\),$/\1/p'

This used sed in “don’t echo lines by default” mode (-n) with a POSIX basic regular expression and a substitution, that basically says: on all lines matching “"USDEUR"”, replace the entire line by everything between the colon and a trailing comma, then output the line.

This won’t match if your line does not have a trailing comma. On the other hand, you seem to know your input is a numeric field, so just exclude commas:

sed -n '/"USDEUR"/s/^.*:\([^,]*\),*$/\1/p'

This replaces everything between a colon and the end of the line that does not contain a colon, and ignores any amount of trailing colons (zero or one, as your input is supposedly valid JSON).

Otherwise, use a JSON parser is the sensible thing to do. You could cobble together more regexes to also catch strings that may contain commas themselves, but… it isn’t worth it at some point, usually.


Add another cut.

grep '"USDEUR" currs.json' | cut -d ':' -f 2 | cut -d',' -f 1

Not the answer you're looking for? Browse other questions tagged .