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
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\.= literalVOL.: the.= any character inregexwithout 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
4 Comments
Kevin
Note that
-P is a gnu flag and not available on most non-Linux systems, e.g. Macs and other BSDs.Gilles Quénot
Yes, but OP have
linux tag ;)Gilles Quénot
Anyway, added a more portable
perl solution.CptNemo
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. )(.*)(?=,)