1

After looking for solutions online I can grep for two hyphens without issue with grep -r "\-\-" or grep -r \\ --. But the files grep searches sometimes have lines full of hyphens, for example in comments, e.g. /*--------- comment ----- */. I want to grep to return cases where only two hyphens are next to each other. E.g. -- and not ---. Ideally I could use -w but I'm not having luck with this.

And a second piece of the puzzle is, if somehow I can grep for only two consecutive hyphens --, I want to find the pattern as though it was a perfect match like -w would do. So something like i--; would not feature as a result. But i -- would. Maybe there's a simple solution but I can't seem to figure it out.

2

At least if you have GNU grep, you could use PCRE lookarounds to match a pair of hyphens that are neither preceded nor followed by a hyphen:

grep -P '(?<!-)--(?!-)' file

or

grep -Pw '(?<!-)--(?!-)' file
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.