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 897 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
![](../../users/profiles/-1.webp)
Community
- 1
- 1
![](../../users/profiles/465183.webp)
Gilles Quenot
- 143,367
- 32
- 199
- 195
-
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 Quenot Apr 20 '13 at 17:12
-
Anyway, added a more portable `perl` solution. – Gilles Quenot 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
![](../../users/profiles/89817.webp)
Gordon Davisson
- 95,980
- 14
- 99
- 125