How to use word boundaries in awk without using match() function?

前端 未结 3 1864
太阳男子
太阳男子 2021-01-14 17:42

I want to add word boundaries to this awk command:

awk \'{$0=tolower($0)};/wordA/&&/wordB/ { print FILENAME \":\" $0; }\' myfile.txt
相关标签:
3条回答
  • 2021-01-14 18:17
    1. GNU awk also supports the \< and \> conventions for word boundaries.
    2. On a Mac, /usr/bin/awk version 20070501 does not support [[:<:]] or [[:>:]]
    3. If you're stuck with a recalcitrant awk, then since awk is normally splitting lines into tokens anyway, it might make sense to use:

      function word(s, i) { for (i=1;i<=NF;i++) {if ($i ~ "^" s "$") {return i}}; return 0; }

    So, for example, instead of writing

    /\<[abc]\>/ { print "matched"; }
    

    you could just as easily write:

    word("[abc]") { print "matched"; }
    
    0 讨论(0)
  • 2021-01-14 18:21

    This might work for you on Mac OS X:

    awk '{$0=tolower($0)};/[[:<:]]wordA[[:>:]]/&&/[[:<:]]wordB[[:>:]]/ { print FILENAME ":" $0; }' myfile.txt
    

    But as it won't work on linux you're best off installing GNU awk.

    0 讨论(0)
  • 2021-01-14 18:32

    You want to use gawk instead of awk:

    gawk '{$0=tolower($0)};/\ywordA\y/&&/\ywordB\y/ { print FILENAME ":" $0; }' myfile.txt
    

    will do what you want, if your system has gawk (e.g. on Mac OS X). \y is a GNU extension to awk.

    0 讨论(0)
提交回复
热议问题