如果你只想要最后一个参数,并且只有一个数字:
echo $'For example:\nThis is counter1 1000\nthis counter2 2000\n
this counter3 is higher value 3000\ndone.\n' |
sed -ne 's/^.* \([0-9]\{1,99\}\)/\1/p'
1000
2000
3000
您甚至可以捕获计数器的编号(因此只读行包含counterX
):
echo $'For example:\nThis is counter1 1000\nthis counter2 2000\n
this counter3 is higher value 3000\ndone.\n' |
sed -ne 's/^.*\(counter[0-9]\{1,99\}\) \(.* \)\{0,1\}\([0-9]\{1,99\}\)$/\1 \3/p'
counter1 1000
counter2 2000
counter3 3000
甚至只是定位分隔符:
echo $'For example:\nThis is counter1 1000\nthis counter2 2000\n
this counter3 is higher value 3000\ndone.\n' |
sed -ne 's/^\(.*counter[0-9]\{1,99\}.*\) \([0-9]\{1,99\}\)$/\1 :: \2/p'
This is counter1 :: 1000
this counter2 :: 2000
this counter3 is higher value :: 3000
或者,或者……
echo $'For example:\nThis is counter1 1000\nthis counter2 2000\n
this counter3 is higher value 3000\ndone.\n' |
sed -e 's/^\(.*counter[0-9]\{1,99\}.*\) \([0-9]\{1,99\}\)$/\1 :: \2/'
For example:
This is counter1 :: 1000
this counter2 :: 2000
this counter3 is higher value :: 3000
done.