Return a regex match in a Bash script, instead of replacing it

前端 未结 9 942
面向向阳花
面向向阳花 2021-01-31 04:30

I just want to match some text in a Bash script. I\'ve tried using sed but I can\'t seem to make it just output the match instead of replacing it with something.



        
9条回答
  •  灰色年华
    2021-01-31 04:54

    I don't know why nobody ever uses expr: it's portable and easy.

    newName()
    {
     #Get input from function
     newNameTXT="$1"
    
     if num=`expr "$newNameTXT" : '[^0-9]*\([0-9]\+\)'`; then
      echo "contains $num"
     fi
    }
    

提交回复
热议问题