Reputation: 1197
I want to use a simple bash syntax to grep numbers from a range. E.g. from the phrase
range "7.2-55.0"
I want to save start=7.2 and end=55.0.
Because I know some perl regex (pcre), I tried:
echo 'range "7.2-55.0"' | pcregrep -o '^range \"(\S+)\"'
echo 'range "7.2-55.0"' | pcregrep -o '^range \"([0-9.-]+)\"'
which isn't working. The output is the whole line. So what is my fault? And is it possible to save 2 matching groups with pcregrep
?
While searching the web I found e.g. pcregrep -o1
but I seem to have another version of the tool, because I am only allowed to use -o
option (GNU Bash-3.2).
Upvotes: 2
Views: 1864
Reputation: 41456
You can do like this with awk
start=$(echo 'range "7.2-55.0"' | awk -F'["-]' '/range/ {print $2}')
end=$(echo 'range "7.2-55.0"' | awk -F'["-]' '/range/ {print $3}')
echo $start
7.2
echo $end
55.0
Upvotes: 1