Timeline for How to run grep with multiple AND patterns?
Current License: CC BY-SA 4.0
19 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jan 13, 2024 at 21:03 | comment | added | jubilatious1 | The accepted answer starts with the phrase, "To find the lines that match each and everyone of a list of patterns... ". So presumably that's what the OP desires. Does perchance this solution return filewise matches instead of linewise matches? If so, it seems this answer could be tweaked to return the latter. | |
| Jul 7, 2022 at 7:27 | comment | added | Mikko Rantalainen | Note that this answer is about searching for all patterns and reporting if each pattern cannot be find at least once in the file. The original question was about matching ALL the patterns against ALL the lines instead of matching files. | |
| Nov 1, 2020 at 15:41 | history | rollback | Noam Manos |
Rollback to Revision 5
|
|
| Nov 1, 2020 at 15:37 | history | rollback | Noam Manos |
Rollback to Revision 3
|
|
| S Oct 30, 2020 at 22:48 | history | edited | G-Man Says 'Reinstate Monica' | CC BY-SA 4.0 |
Used more standard formatting.
|
| S Oct 30, 2020 at 22:48 | history | suggested | Peter Mortensen | CC BY-SA 4.0 |
Used more standard formatting (we have italics and bold on this platform). Removed historical information (that is what the revision history is for) - the answer should be as if it was written today. Expanded.
|
| Oct 30, 2020 at 20:43 | review | Suggested edits | |||
| S Oct 30, 2020 at 22:48 | |||||
| Aug 23, 2018 at 7:58 | comment | added | Noam Manos | @greenholdman, why do you think it doesn't add anything? It's a great solution to verify numerous words/regexs. Imagine grep -e 'pattern1.*pattern2' -e 'pattern2.*pattern1' on 10+ regex, not just two... | |
| Aug 20, 2018 at 16:40 | review | Low quality posts | |||
| Aug 20, 2018 at 16:42 | |||||
| Aug 20, 2018 at 16:38 | comment | added | greenoldman |
Of course, I could add variable tracking whether AND condition is fulfilled, and then I would have an extra script instead of short and concise call of grep which was posted and accepted as solution six years ago. Take signal to noise into consideration and please delete your entire answer -- it does not add anything really.
|
|
| Aug 20, 2018 at 9:51 | comment | added | Noam Manos |
@greenoldman sorry I don't get your point. ^a+$ , ^b+$ , ^h+$ are all positive match, but ^d+$ is not a match, so the search then breaks. That's exactly the meaning of AND condition! You don't have to print anything during the loop, just after it ends, if that's what you want.
|
|
| Aug 20, 2018 at 5:56 | comment | added | greenoldman |
You have it right in front of your eyes, you have positive match just after first match is executed. You should have "collect" all outcomes and compute AND on them. Then you should rewrite the script to run on multiple files -- then maybe you realize that the question is already answered and your attempt does not bring anything to the table, sorry.
|
|
| Aug 19, 2018 at 15:12 | history | edited | Noam Manos | CC BY-SA 4.0 |
added 66 characters in body
|
| Aug 19, 2018 at 15:04 | comment | added | Noam Manos | @greenoldman not sure why you think this loop does not check AND condition for all patterns? So I've edited my answer with a real example: It will search in file for all regex of list, and on the first one which is missing - will exit with error. | |
| Aug 19, 2018 at 14:56 | history | edited | Noam Manos | CC BY-SA 4.0 |
For those who still think it's not working, here's a real example
|
| Aug 17, 2018 at 6:19 | comment | added | greenoldman |
I understand your logic as well as my question -- I was asking about AND operator, meaning the file is only a positive hit if it matches pattern A and pattern B and pattern C and... AND In you case file is positive hit if it matches pattern A or pattern B or... Do you see the difference now?
|
|
| Aug 16, 2018 at 15:07 | comment | added | Noam Manos | @greenoldman The logic is simple: The for will loop on ALL of the words/patterns in the list, and if it is found in file - will print it. So just remove the else if you don't need action in case word was not found. | |
| Aug 14, 2018 at 22:18 | comment | added | greenoldman |
Your logic is faulty -- I asked for ALL operator, your code works as OR operator, not AND. And btw. for that (OR) is much easier solution given right in the question.
|
|
| Aug 14, 2018 at 6:51 | history | answered | Noam Manos | CC BY-SA 4.0 |