Search code examples
awkoperatorscase-insensitive

awk case insensitive and boolean operator


I would like to grep lines that contains both patterns in any order and I'm using

awk '/pattern1/ && /pattern2/' file.txt

but if I want to do case insensitive search, adding /i works only if I add it to pattern2.

awk '/pattern1/ && /pattern2/i' file.txt ...works

awk '/pattern1/i && /pattern2/i' file.txt ...don't, outputs the whole file

anyone know how to solve this?


Solution

  • Try:

    awk '{s=tolower($0)} s~/lowercase_pattern1/ && s~/lowercase_pattern2/' file
    

    There is also the possibility of the IGNORECASE option in GNU awk..


    You could also most of the time do something like this:

    grep -Ei 'pattern1.*pattern2|pattern2.*pattern1' file