1

My String is:

model_config_list: {
   config: {
       name: "123-model",
       base_path: "/modelServers/tests-integration/resources/models/123-model",
       model_platform: "tensorflow",
       model_version_policy: {
           all: {}
       }
   }

I want to extract:

/modelServers/tests-integration/resources/models/123-model

for that I wrote bash script :

config_file=$(<conf.conf)
regex="(?<=base_path:)(.*)(?=,)"
if [[ $config_file =~ $regex ]];then echo ${BASH_REMATCH[1]}; fi

However, I am not getting any output. I am a beginner with Bash scripting.

2 Answers 2

3

You need to use a regex with a capturing group rather than lookarounds that are not supported in Bash regex:

regex='base_path:[[:space:]]*"([^"]+)"'
if [[ $config_file =~ $regex ]];then 
  echo ${BASH_REMATCH[1]};
fi

Output:

/modelServers/tests-integration/resources/models/123-model

See the Bash demo online.

Details

The positive (?<=base_path:) lookbehind and (?=,) lookahead are not supported in Bash regex. The point here is to match base_path: with any whitespace after it, then ", and then capture into Group 1 any one or more chars other than " (using a negated bracket expression [^"]+).

  • base_path: - a literal substring
  • [[:space:]]* - 0+ whitespace chars
  • " - a double quote
  • ([^"]+) - 1 or more chars other than a " char
  • " - a double quote.
Sign up to request clarification or add additional context in comments.

Comments

2

Bash doesn't support lookaround assertions. But you don't need them:

regex='base_path: "([^"]*)"'

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.