extract regexp result from string and write it to a variable

前端 未结 5 1451
星月不相逢
星月不相逢 2021-01-24 19:05

I\'m trying to write a shell script that searches in a file specified as argument $1 for a regex and writes the found subpattern into a variable I can then use.

let\'s s

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-24 19:12

    dosth.sh:

    #!/bin/bash
    VALUE=`cat $1 | grep `
    echo $VALUE
    

    this can be used as:

    $ ./dosth.sh a.txt
    

    then the found strings are stored in the variable VALUE

    You can also give the regexp over by some arguments (asked for the filename by the script):

    dosth.sh:

    #!/bin/bash
    echo -e "Which file do you want to grep?"
    read  FILENAME
    args=("$@")
    VALUE=`cat $FILENAME | grep $args`
    echo $VALUE
    

    which can be used like:

    $ ./dosth.sh here comes my * pattern
    

    Maybe this link is helpful for you: Link

提交回复
热议问题