sed: print only matching group

前端 未结 5 1338
伪装坚强ぢ
伪装坚强ぢ 2020-12-23 02:26

I want to grab the last two numbers (one int, one float; followed by optional whitespace) and print only them.

Example:

foo bar  bla 1 2 3         


        
5条回答
  •  醉话见心
    2020-12-23 03:01

    Match the whole line, so add a .* at the beginning of your regex. This causes the entire line to be replaced with the contents of the group

    echo "foo bar  bla 1 2 3.4" |
     sed -n  's/.*\([0-9][0-9]*[\ \t][0-9.]*[ \t]*$\)/\1/p'
    2 3.4
    

提交回复
热议问题