admin管理员组

文章数量:1122846

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?

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?

Share Improve this question asked Nov 22, 2024 at 0:32 joseluisbzjoseluisbz 1,6262 gold badges38 silver badges63 bronze badges 1
  • I see you never accept answers to any of your questions. YMMV getting answers in future if you never accept any of them, see stackoverflow.com/help/someone-answers. – Ed Morton Commented Nov 24, 2024 at 13:07
Add a comment  | 

2 Answers 2

Reset to default 0

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 }
' {} +

You can use xargs to avoid repetition:

echo OX PP SPAD | 
    xargs -n 1 -I {} grep -RHl ',"estadoOperacion":"{}"' ~/Company/logs-delete/pf* |
    sort | uniq -c | awk '$1 == 3{ print $2 }'

本文标签: