0

I am using this sed command to extract a json value for a key, say MyKey..

sedPattern='s/^.*"myKey":"\([^"]*\)".*$/\1/'

For a json string like,

{"myKey" : 1, "yourKey": 2}

It returns what i need,

1

But what does \1 action in the above sed command mean?

3

\([^"]*\) defines a group of characters, the group is actually the found contents between the two braces (), the braces need to be escaped hence \( and \)

\1 is the contents of the first group

There could be several groups defined using multiple sets of brace pairs, they are numbered incrementally and each one can be referenced as \n (e.g. \1, \2, \3 etc) hence the name: backreference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.