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.
Asked
Active
Viewed 914 times
2 Answers
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\.
= literalVOL.
: the.
= any character inregex
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
-
2Note that `-P` is a gnu flag and not available on most non-Linux systems, e.g. Macs and other BSDs. – Kevin Apr 20 '13 at 15:15
-
Yes, but OP have `linux` tag ;) – Gilles Quénot Apr 20 '13 at 17:12
-
Anyway, added a more portable `perl` solution. – Gilles Quénot Apr 20 '13 at 17:13
-
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. )(.*)(?=,) – CptNemo Apr 20 '13 at 21:58
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