1

I am pretty newe to linux and even though I need something simple I dont know where to start. In a bash script I need to parse the value from a HTML page between the string "VOL. " and "," and pass it to a variable.

2 Answers 2

4
newvar=$(grep -oP 'VOL\.\K.*?(?=,)' file.txt)
echo "$newvar"

or from a string :

newvar=$(grep -oP 'VOL\.\K.*?(?=,)' <<< "$string")
echo "$newvar"

if you need something more portable :

newvar=$(perl -lne '/VOL\.\K.*?(?=,)/ && print $&' <<< "$string")
echo "$newvar"

Explanations of the Regex

  • VOL\. = literal VOL. : the . = any character in regex without backslash
  • \K = restart the match to zero, see https://stackoverflow.com/a/13543042/465183
  • .*? = any character, 0 to N occurrences but non-greedy with ? char
  • (?=,) = it's a positive look-ahead assertion to look up the , char
Sign up to request clarification or add additional context in comments.

4 Comments

Note that -P is a gnu flag and not available on most non-Linux systems, e.g. Macs and other BSDs.
Yes, but OP have linux tag ;)
Anyway, added a more portable perl solution.
Thanks sputnick, that works. Just for the sake of understanding it is 'VOL\.\K.*?(?=,)' regex? Because I tried to make this expressions works with no success: (?<=VOL. )(.*)(?=,)
1

This can be done using bash's built-in regex matching:

if [[ "$var" =~ "VOL. "([^,]*)"," ]]; then
    match="${BASH_REMATCH[1]}"
fi

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.