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

前端 未结 5 529
爱一瞬间的悲伤
爱一瞬间的悲伤 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:20

    If you want to do it in Bash, you could do something like the following. It uses globbing instead of regexps (The extglob shell option enables extended pattern matching, so that we can match a line consisting only of asterisks.)

    #!/bin/bash
    shopt -s extglob
    entry=""
    while read line
    do
        case $line in 
            +(\*))
                # do something with $entry here
                entry=""
                ;;
            *)
                entry="$entry$line
    "
                ;;
        esac
    done
    

提交回复
热议问题