macosterminalgrep

Finding All multiple patterns in files inside of a folder with grep, using macOS


I would like to obtain the files that contains all patterns Simultaneously!

the-user@MAC-001 logs-delete % grep -RHl ",\"estadoOperacion\":\"PP\"" ~/Company/logs-delete/pf*
/Users/the-user/logs-delete/pf_3.0_20241024_131845.log
/Users/the-user/logs-delete/pf_3.0_20241025_075400.log
/Users/the-user/logs-delete/pf_3.0_20241028_141054.log
/Users/the-user/logs-delete/pf_3.0_20241029_083212.log
/Users/the-user/logs-delete/pf_3.0_20241029_124232.log
/Users/the-user/logs-delete/pf_3.0_20241104_103451.log
/Users/the-user/logs-delete/pf_3.0_20241106_073739.log
/Users/the-user/logs-delete/pf_3.0_20241106_124513.log
/Users/the-user/logs-delete/pf_3.0_20241107_094139.log
/Users/the-user/logs-delete/pf_3.0_20241108_065032.log
/Users/the-user/logs-delete/pf_3.0_20241108_094537.log
/Users/the-user/logs-delete/pf_3.0_20241108_154655.log
the-user@MAC-001 logs-delete % grep -RHl ",\"estadoOperacion\":\"OX\"" ~/Company/logs-delete/pf*
/Users/the-user/logs-delete/pf_3.0_20241028_102403.log
/Users/the-user/logs-delete/pf_3.0_20241028_141054.log
/Users/the-user/logs-delete/pf_3.0_20241028_143253.log
/Users/the-user/logs-delete/pf_3.0_20241029_065826.log
/Users/the-user/logs-delete/pf_3.0_20241029_083212.log
/Users/the-user/logs-delete/pf_3.0_20241029_124232.log
/Users/the-user/logs-delete/pf_3.0_20241029_160927.log
/Users/the-user/logs-delete/pf_3.0_20241030_065136.log
/Users/the-user/logs-delete/pf_3.0_20241030_072228.log
the-user@MAC-001 logs-delete % grep -RHl ",\"estadoOperacion\":\"SPAD\"" ~/Company/logs-delete/pf*
/Users/the-user/logs-delete/pf_3.0_20241029_083212.log
/Users/the-user/logs-delete/pf_3.0_20241029_124232.log
/Users/the-user/logs-delete/pf_3.0_20241106_124513.log
/Users/the-user/logs-delete/pf_3.0_20241108_065032.log
/Users/the-user/logs-delete/pf_3.0_20241108_094537.log
/Users/the-user/logs-delete/pf_3.0_20241108_154655.log
the-user@MAC-001 logs-delete %

I was trying with:

the-user@MAC-001 logs-delete % 
grep -RHl -e ",\"estadoOperacion\":\"SR\""  -e ",\"estadoOperacion\":\"OAD\""  -e ",\"estadoOperacion\":\"PP\""  -e ",\"estadoOperacion\":\"SPAD\""   -e ",\"estadoOperacion\":\"OX\"" ~/Company/logs-delete/pf*


/Users/the-user/logs-delete/pf_3.0_20241001_072248.log
/Users/the-user/logs-delete/pf_3.0_20241002_153232.log
/Users/the-user/logs-delete/pf_3.0_20241003_075357.log
/Users/the-user/logs-delete/pf_3.0_20241003_113433.log
/Users/the-user/logs-delete/pf_3.0_20241003_152330.log
/Users/the-user/logs-delete/pf_3.0_20241004_075536.log
/Users/the-user/logs-delete/pf_3.0_20241007_142857.log
/Users/the-user/logs-delete/pf_3.0_20241007_144142.log
/Users/the-user/logs-delete/pf_3.0_20241008_114436.log
/Users/the-user/logs-delete/pf_3.0_20241008_144330.log
/Users/the-user/logs-delete/pf_3.0_20241008_172634.log
/Users/the-user/logs-delete/pf_3.0_20241009_095809.log
/Users/the-user/logs-delete/pf_3.0_20241009_213218.log
/Users/the-user/logs-delete/pf_3.0_20241010_074449.log
/Users/the-user/logs-delete/pf_3.0_20241010_144036.log
/Users/the-user/logs-delete/pf_3.0_20241011_113226.log
/Users/the-user/logs-delete/pf_3.0_20241011_160243.log
/Users/the-user/logs-delete/pf_3.0_20241014_095831.log
/Users/the-user/logs-delete/pf_3.0_20241015_111718.log
/Users/the-user/logs-delete/pf_3.0_20241016_070147.log
/Users/the-user/logs-delete/pf_3.0_20241016_094720.log
/Users/the-user/logs-delete/pf_3.0_20241016_115045.log
/Users/the-user/logs-delete/pf_3.0_20241016_145233.log
/Users/the-user/logs-delete/pf_3.0_20241016_151614.log
/Users/the-user/logs-delete/pf_3.0_20241016_215805.log
/Users/the-user/logs-delete/pf_3.0_20241017_072014.log
/Users/the-user/logs-delete/pf_3.0_20241017_120459.log
/Users/the-user/logs-delete/pf_3.0_20241017_182521.log
/Users/the-user/logs-delete/pf_3.0_20241018_105402.log
/Users/the-user/logs-delete/pf_3.0_20241021_075142.log
/Users/the-user/logs-delete/pf_3.0_20241021_103556.log
/Users/the-user/logs-delete/pf_3.0_20241021_145449.log
/Users/the-user/logs-delete/pf_3.0_20241022_090917.log
/Users/the-user/logs-delete/pf_3.0_20241022_104625.log
/Users/the-user/logs-delete/pf_3.0_20241022_133342.log
/Users/the-user/logs-delete/pf_3.0_20241023_101721.log
/Users/the-user/logs-delete/pf_3.0_20241024_081816.log
/Users/the-user/logs-delete/pf_3.0_20241024_100650.log
/Users/the-user/logs-delete/pf_3.0_20241024_131845.log
/Users/the-user/logs-delete/pf_3.0_20241025_070917.log
/Users/the-user/logs-delete/pf_3.0_20241025_075400.log
/Users/the-user/logs-delete/pf_3.0_20241028_102403.log
/Users/the-user/logs-delete/pf_3.0_20241028_141054.log
/Users/the-user/logs-delete/pf_3.0_20241028_143253.log
/Users/the-user/logs-delete/pf_3.0_20241029_065826.log
/Users/the-user/logs-delete/pf_3.0_20241029_083212.log
/Users/the-user/logs-delete/pf_3.0_20241029_124232.log
/Users/the-user/logs-delete/pf_3.0_20241029_160927.log
/Users/the-user/logs-delete/pf_3.0_20241030_065136.log
/Users/the-user/logs-delete/pf_3.0_20241030_072228.log
/Users/the-user/logs-delete/pf_3.0_20241104_065520.log
/Users/the-user/logs-delete/pf_3.0_20241104_103451.log
/Users/the-user/logs-delete/pf_3.0_20241106_065517.log
/Users/the-user/logs-delete/pf_3.0_20241106_073739.log
/Users/the-user/logs-delete/pf_3.0_20241106_124513.log
/Users/the-user/logs-delete/pf_3.0_20241106_161638.log
/Users/the-user/logs-delete/pf_3.0_20241107_070046.log
/Users/the-user/logs-delete/pf_3.0_20241107_094139.log
/Users/the-user/logs-delete/pf_3.0_20241108_065032.log
/Users/the-user/logs-delete/pf_3.0_20241108_094537.log
/Users/the-user/logs-delete/pf_3.0_20241108_154655.log
/Users/the-user/logs-delete/pf_3.0_20241121_113359.log

As you can see the behavior is like an OR, not like an AND.

How fix my sentence in macOS to obtain all files containing all words/patterns simultaneously?


Solution

  • Don't use grep (g/re/p) to find files. There's a perfectly good command to find files with an extremely obvious name. Also, awk is better than grep for doing g/re/p when the re requires multiple matches in any order. Try this:

    find /Company/logs-delete -type f -name 'pf*' -exec awk '
        FNR == 1 { a=b=c=0 }
        /,"estadoOperacion":"PP"/   {a=1}
        /,"estadoOperacion":"OX"/   {b=1}
        /,"estadoOperacion":"SPAD"/ {c=1}
        a && b && c { print FILENAME; nextfile }
    ' {} +
    

    That assumes your awk supports nextfile. If it doesn't then get a newer version of awk but, meantime, this will do the same in any awk but just run a bit slower:

    find /Company/logs-delete -type f -name 'pf*' -exec awk '
        FNR == 1 { a=b=c=done=0 }
        done { next }
        /,"estadoOperacion":"PP"/   {a=1}
        /,"estadoOperacion":"OX"/   {b=1}
        /,"estadoOperacion":"SPAD"/ {c=1}
        a && b && c { print FILENAME; done=1 }
    ' {} +