EverythingRightPlace
EverythingRightPlace

Reputation: 1197

Pcregrep using matching groups

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

Answers (1)

Jotne
Jotne

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

Related Questions