-1

How do I achieve the following in Shell Script (bourne and/or bash)... something similar to what I can, using RegEx on a file (content sample below)

  1. I need to match the pattern, and then -
  2. Capture the content to the right side of 'LD_PRELOAD=' into a 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 the online Regex Builder (https://regex101.com/r/XW0JVR/1)

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

I tried the following using Shell Script, but it doesn't work...

#!/bin/bash
envvars_stdFile="tempEnvVarsStd"

regEx="LD_PRELOAD=(\S+)[\n\r]+^\s*export\s+LD_PRELOAD"

if [[ "${envvars_stdFile}" =~ "${regEx}" ]]
then
    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

MaxGrand
  • 103
  • 8
  • What you're asking for is unclear, what is your end goal with this script? Have you written any code to show as an example? – Ulises André Fierro Jul 25 '17 at 17:25
  • My question is basically about Multi Line search in a file using Shell Scripting (bourne or bash). As indicated in the OP, I need to first match the lines and then capture the group of interest into a variable. – MaxGrand Jul 25 '17 at 17:29
  • There are several things to consider and several ways to go on about it but I doubt that bash will be your best option. As far as the regex, is that necessary? If all you need is to match "LD_PRELOAD" you can first just match that.. (Using awk, grep, or any other tool for that matter) Then simplify your expression to match whatever is after the "=". – Ulises André Fierro Jul 25 '17 at 17:32
  • I need to match "LD_PRELOAD=" and "export LD_PRELOAD" lines. That's why I thought RegEx is straight-forward... – MaxGrand Jul 25 '17 at 17:34
  • By the way, following shell script code snippet doesn't work for me... #!/bin/bash envvars_stdFile="tempEnvVarsStd" regEx="LD_PRELOAD=(\S+)[\n\r]+^\s*export\s+LD_PRELOAD" if [[ "${envvars_stdFile}" =~ "${regEx}" ]] then 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" – MaxGrand Jul 25 '17 at 17:35
  • Please append that to your question so everyone can see it – Ulises André Fierro Jul 25 '17 at 17:36
  • With GNU grep: `variable=$(grep -Po '^LD_PRELOAD=\K.*' file)` – Cyrus Jul 25 '17 at 17:39
  • @Cyrus Thanks, but I need to make sure the following line 'export LD_PRELOAD' also exists with my check... Is there a way to combine both ? – MaxGrand Jul 25 '17 at 17:44
  • 1
    Try this with GNU grep: `variable=$(grep -Poz '^LD_PRELOAD=\K.*(?=(\n.*)*^export LD_PRELOAD$)' file)` – Cyrus Jul 25 '17 at 17:55
  • Tip: You can make life easier on yourself by starting small. If you try the regex `LD_PRELOAD` or even `.*` in your code, you'll find that it doesn't match either. – that other guy Jul 25 '17 at 17:59
  • @cyrus Thanks. your latest update gets the job done... Can you help explain your RegEx ? Also, how do I mark your response as a working answer/solution? Sorry I'm newbie to SO. – MaxGrand Jul 25 '17 at 18:04
  • 1
    As first step: `grep -Poz 'LD_PRELOAD=.*(\n.*)*export LD_PRELOAD' file`. This grabs your line with `LD_PRELOAD=`, line with `export LD_PRELOAD` and all lines between those lines. Next step: `\K` removes matching part before `\K` and `(?=regex)` removes part that matches this regex. It remains only part after `=` in first line. See: [The Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/a/22944075/3776858) and `man grep` for options `-P` `-o` and `-z`. – Cyrus Jul 25 '17 at 18:17
  • @cyrus That helps! Now, I would really like your solution more visible... can you paste your solution as "an answer" to my question? so I can rank it as "working" ? – MaxGrand Jul 25 '17 at 18:55

1 Answers1

0

Thanks to @Cyrus. I ended up using his suggestion. Posting his suggestion here for more visibility, for others looking for similar question of Shell Script MultiLine RegEx...

variable=$(grep -Poz '^LD_PRELOAD=\K.*(?=(\n.*)*^export LD_PRELOAD$)' file)
MaxGrand
  • 103
  • 8