How to print matched regex pattern using awk?

后端 未结 8 667
说谎
说谎 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:46

    This is the very basic

    awk '/pattern/{ print $0 }' file
    

    ask awk to search for pattern using //, then print out the line, which by default is called a record, denoted by $0. At least read up the documentation.

    If you only want to get print out the matched word.

    awk '{for(i=1;i<=NF;i++){ if($i=="yyy"){print $i} } }' file
    

提交回复
热议问题