how to use sed, awk, or gawk to print only what is matched?

后端 未结 11 491
长情又很酷
长情又很酷 2021-01-30 05:59

I see lots of examples and man pages on how to do things like search-and-replace using sed, awk, or gawk.

But in my case, I have a regular expression that I want to run

11条回答
  •  一向
    一向 (楼主)
    2021-01-30 06:55

    You can use sed to do this

     sed -rn 's/.*abc([0-9]+)xyz.*/\1/gp'
    
    • -n don't print the resulting line
    • -r this makes it so you don't have the escape the capture group parens().
    • \1 the capture group match
    • /g global match
    • /p print the result

    I wrote a tool for myself that makes this easier

    rip 'abc(\d+)xyz' '$1'
    

提交回复
热议问题