简体   繁体   中英

Get only numbers in output

I need to get only numbers from this: release/M_0.1.0 thus, need to extract with bash to have in output this: 0.1.0 . I have tried this but cannot finish it: echo "release/M_0.1.0" | awk -F'/' '{print $2}' echo "release/M_0.1.0" | awk -F'/' '{print $2}'

And what about if given such string? relea234se/sdf23_4Mm0.1.0.8 . How to get only 0.1.0.8 ? Please pay attention that this can be random digits such as 0.2 or 1.9.1 .

You could also use general parameter expansion parsing to literally remove characters up through the last that isn't digits or dots.

$: ver() { echo "${1//*[^.0-9]/}"; }
$: ver release/M_0.1.0
0.1.0
$: ver relea234se/sdf23_4Mm0.1.0.8
0.1.0.8

请检查此grep命令是否有效

echo "release/M_0.1.0" | egrep -o '[0-9.]+'

With sed you can do:

echo "release/M_0.1.0" | sed 's@.*_@@'

Output :

0.1.0

Considering that your Input_file will be same as shown samples.

echo "$var" | awk -F'_' '{print $2}'

OR could use sub :

echo "$var" | awk '{sub(/.*_/,"")} 1'

With simple bash you could use:

echo "${var#*_}"
echo release/M_0.1.0 | awk -F\_ '{print $2}'
0.1.0

Take your pick:

$ var='relea234se/sdf23_4Mm0.1.0.8'

$ [[ $var =~ .*[^0-9.](.*) ]] && echo "${BASH_REMATCH[1]}"
0.1.0.8

$ echo "$var" | sed 's/.*[^0-9.]//'
0.1.0.8

$ echo "$var" | awk -F'[^0-9.]' '{print $NF}'
0.1.0.8

如果数据在d文件中,请尝试使用 gnu sed:

sed -E 's/relea.*/.*([0-9][0-9.]*)$/\1/' d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM