Using sed with capture groups

There are times where I would like to use capture groups in a tool like grep when searching through text. Today I learned that you can accomplish with sed. Note that this is a quick summary based on the longer sed tutorial from grymoire.

First, create a json file to parse.

# create a file
cat <<EOF > result.json
{
  "id": "123",
  "name": "curtis"
}
EOF

Next, parse and return the value from the name key.

sed -n 's/[ \t]*"name": "\(.*\)",\?/\1/p' result.json

Interpretation is as follows.

NOTE:

  • Any part of the pattern (i.e. line) not included will be printed out thus the entire pattern must be captured.

REGEX:

  • [ \t]* ==> One or more tabs and/or spaces

  • "\(.*\)" ==> The escaped parentheses represent the actual capture group

  • \1 ==> The actual captured result

  • ,\? ==> Match comma zero or one time

SWITCHES:

  • s/ ==> substitute - because using substitute, need to ensure that the entire pattern (i.e. line) is captured

  • -n ==> no printing except when p is included - when used in conjunction with /p, acts like grep

  • /p ==> print match (substitution)

Why not just use jq? jq is great! But sometimes you just want to utilize the tools you have at hand to get the job done!