sed: print only matching group

前端 未结 5 1334
伪装坚强ぢ
伪装坚强ぢ 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:06

    And for yet another option, I'd go with awk!

    echo "foo bar  bla 1 2 3.4" | awk '{ print $(NF-1), $NF; }'
    

    This will split the input (I'm using STDIN here, but your input could easily be a file) on spaces, and then print out the last-but-one field, and then the last field. The $NF variables hold the number of fields found after exploding on spaces.

    The benefit of this is that it doesn't matter if what precedes the last two fields changes, as long as you only ever want the last two it'll continue to work.

提交回复
热议问题