bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin19)
Description:
I want to extract the WorkingDir
key value from this string dictionary:
config="""
"TerraformCommand": "terragrunt-info",
"WorkingDir": "usr/terraform-modules/terraform-aws-codebuild/examples/.terragrunt-cache/xh9X2WgTwVjjRHiPBjKUl0Lr86w/SGN8gG45haGoXT7IhOh9_iuKkbc"
}
"""
In this case, the expected output would be:
"usr/terraform-modules/terraform-aws-codebuild/examples/.terragrunt-cache/xh9X2WgTwVjjRHiPBjKUl0Lr86w/SGN8gG45haGoXT7IhOh9_iuKkbc"
Attempts:
So far I've tried using different methods using this positive lookbehind/lookahead pattern: '(?<=("WorkingDir":s")).+(?=")'
1.
echo `expr "$config" : '(?<=("WorkingDir":s")).+(?=")'`
Output: 0
2.
pat='(?<=("WorkingDir":s")).+(?=")'
[[ $config =~ $pat ]]
echo "${BASH_REMATCH[0]}"
echo "${BASH_REMATCH[1]}"
output:
echo $config | grep -o '(?<=("WorkingDir":s")).+(?=")'
output:
question from:
https://stackoverflow.com/questions/65855862/bash-use-match-regex-with-lookbehind-pattern-on-string 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…