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

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

    depending on what you want to do with the variables

    awk '
    f && /\*/{print "variable:"s;f=0}
    /\*/{ f=1 ;s="";next}
    f{
       s=s" "$0
    }' file
    

    output:

    # ./test.sh
    variable: Field1
    variable: Lorem ipsum Data to match
    variable: More data Still more data
    

    the above just prints them out. if you want, store in array for later use...eg array[++d]=s

提交回复
热议问题