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

前端 未结 9 763
面向向阳花
面向向阳花 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 05:00

    echo "TestT100String" | sed 's/[^0-9]*\([0-9]\+\).*/\1/'
    
    echo "TestT100String" | grep -o  '[0-9]\+'
    

    The method you use to put the results in an array depends somewhat on how the actual data is being retrieved. There's not enough information in your question to be able to guide you well. However, here is one method:

    index=0
    while read -r line
    do
        array[index++]=$(echo "$line" | grep -o  '[0-9]\+')
    done < filename
    

    Here's another way:

    array=($(grep -o '[0-9]\+' filename))
    

提交回复
热议问题