How to print matched regex pattern using awk?

后端 未结 8 682
说谎
说谎 2020-11-29 15:56

Using awk, I need to find a word in a file that matches a regex pattern.

I only want to print the word matched with the pattern.

So if

8条回答
  •  时光说笑
    2020-11-29 16:31

    gawk can get the matching part of every line using this as action:

    { if (match($0,/your regexp/,m)) print m[0] }
    

    match(string, regexp [, array]) If array is present, it is cleared, and then the zeroth element of array is set to the entire portion of string matched by regexp. If regexp contains parentheses, the integer-indexed elements of array are set to contain the portion of string matching the corresponding parenthesized subexpression. http://www.gnu.org/software/gawk/manual/gawk.html#String-Functions

提交回复
热议问题