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.

CptNemo
  • 6,455
  • 16
  • 58
  • 107

2 Answers2

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
Community
  • 1
  • 1
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

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

if [[ "$var" =~ "VOL. "([^,]*)"," ]]; then
    match="${BASH_REMATCH[1]}"
fi
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151