Shell script (bourne and/or bash) RegEx over Multiple Lines -


how achieve following in shell script (bourne and/or bash)... similar can, using regex on file (content sample below)

  1. i need match pattern, , -
  2. capture content right side of 'ld_preload=' variable.

file content:

blah blah blah #some comment ld_preload=/usr/lib64/libstdc++.so.6  export ld_preload  #more comments 

the following regex works fine on online regex builder (https://regex101.com/r/xw0jvr/1)

/ld_preload=(\s+)[\n\r]+^\s*export\s+ld_preload/gm 

i tried following using shell script, doesn't work...

#!/bin/bash envvars_stdfile="tempenvvarsstd"  regex="ld_preload=(\s+)[\n\r]+^\s*export\s+ld_preload"  if [[ "${envvars_stdfile}" =~ "${regex}" ]]     cpplib="${bash_rematch[1]}"     echo "cpplib is: $cpplib" else    echo "regex pattern (${regex}) doesn't match in file: $envvars_stdfile" fi 

it produces: regex pattern (ld_preload=(\s+)[\n\r]+^\s*export\s+ld_preload) doesn't match in file: tempenvvarsstd

thanks @cyrus. ended using suggestion. posting suggestion here more visibility, others looking similar question of shell script multiline regex...

variable=$(grep -poz '^ld_preload=\k.*(?=(\n.*)*^export ld_preload$)' file) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -