How can I assign the match of my regular expression to a variable?

前端 未结 5 534
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-07 06:34

I have a text file with various entries in it. Each entry is ended with line containing all asterisks.

I\'d like to use shell commands to parse this file and assign each

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-07 07:22

    I'm surprised to not see a native bash solution here. Yes, bash has regular expressions. You can find plenty of random documentation online, particularly if you include "bash_rematch" in your query, or just look at the man pages. Here's a silly example, taken from here and slightly modified, which prints the whole match, and each of the captured matches, for a regular expression.

    if [[ $str =~ $regex ]]; then
        echo "$str matches"
        echo "matching substring: ${BASH_REMATCH[0]}"
        i=1
        n=${#BASH_REMATCH[*]}
        while [[ $i -lt $n ]]
        do
            echo "  capture[$i]: ${BASH_REMATCH[$i]}"
            let i++
        done
    else
        echo "$str does not match"
    fi
    

    The important bit is that the extended test [[ ... ]] using its regex comparision =~ stores the entire match in ${BASH_REMATCH[0]} and the captured matches in ${BASH_REMATCH[i]}.

提交回复
热议问题