sed: print only matching group

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

    I agree with @kent that this is well suited for grep -o. If you need to extract a group within a pattern, you can do it with a 2nd grep.

    # To extract \1 from /xx([0-9]+)yy/
    $ echo "aa678bb xx123yy xx4yy aa42 aa9bb" | grep -Eo 'xx[0-9]+yy' | grep -Eo '[0-9]+'
    123
    4
    
    # To extract \1 from /a([0-9]+)b/
    $ echo "aa678bb xx123yy xx4yy aa42 aa9bb" | grep -Eo 'a[0-9]+b' | grep -Eo '[0-9]+'
    678
    9
    

提交回复
热议问题