1

i need a string checked for an Regex and the Match should be returned into a variable.

I read line by line through the data, and for that, i have some data i have to extract from that line. I checked that line for an value, if that is true, i need the regexp match as result back, to get stored in a value.

All this have to be done in a bash.

Is there a good approach for that ?

1 Answer 1

1

You can use the =~ operator in a [[ .. ]] condition. Round parentheses introduce capture groups, they populate the BASH_REMATCH array if matched.

#! /bin/bash
while read line ; do
    if [[ $line =~ d(.)f ]] ; then
        matched=${BASH_REMATCH[1]}
    fi
done < <(printf '%s\n' abc def ghi)
echo "Matched $matched."

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .