4 ms·
I have a list of gotchas and tricks here: https://learnbyexample.github.io/learn_gnugrep_ripgrep/gotchas-and-tricks.html https://learnbyexample.github.io/learn_
by asicsp 6y ago
I have a list of gotchas and tricks here: https://learnbyexample.github.io/learn_gnugrep_ripgrep/gotchas-and-tricks.html https://learnbyexample.github.io/learn_gnugrep_ripgrep/gotch...
As pointed out in other comments, many of the issues in the post is due to shell, not specific to grep. Especially quoting. Always use single quotes to specify the search pattern, unless other forms of shell quoting is needed. Otherwise, you'll face issues with commands like
grep ; ip.txt
Another example is searching for a pattern that starts with a hyphen, which causes issue even with quoting
$ echo '5*3-2=13' | grep '-2'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
You'll need to either escape the hyphen or use -- before the search pattern to prevent it from being treated as a command option. This is needed if a filename starts with a hyphen too.
- xorcist 6y agoThat's a really good article! One of the most common mistakes I see is missing escaping. Not strange when the rules change between the various regexp modes. Something like 'grep "file.exe"' is probably not what you want, and sometimes it can be easier to turn off regexp processing with -F in the the cases that doesn't use them.