How to Exclude a Word with grep
How to Exclude a Word with grep
The grep command line tool is wildly useful for searching through text data for lines and snippets that match a defined string, character, word, or regular expression. While most uses of grep are for sorting data for syntax matches, what if you want to instead? Excluding line matches with grep is equally as useful as finding and printing matches in grep, so lets cover how to exclude string matches and exclude words with grep. Obviously youll want to have some experience and exposure to grep to find this useful. If you want to follow along, you can open the Terminal application and try it out yourself. Since grep is an OS agnostic utility, you can use the exclude trick in Mac OS, Linux, unix, or whatever else you have that uses grep. The most simple way to exclude lines with a string or syntax match is by using grep and the -v flag. For example, lets say were using cat to print a file at the command line, but we want to exclude all lines that include the term ThisWord, then the syntax would look as follow: The output will be the example.txt text file but excluding any line that contains a string match with ThisWord. You can also use grep directly on files and exclude line matches based on words or syntax, like so: Use whichever works best for your particular workflow. Now that you know how to exclude matches for a single word, the next obvious question is about excluding multiple words with grep. Thats equally as simple, and there are a few different ways to accomplish this using the -v flag as well as the -e flag. First lets take the above example of using cat on a file piped to grep, and exclude any lines matching two words; Word1 and Word2, this would look like the following: Any lines that contain Word1 or Word2 will be excluded from the printed results. You can also use grep directly on files just as before as well: Another approach is to separate what to exclude with grep by using a pipe to separate each match, like so: If you test out any of these options on an example text file, you will find the output is identical regardless of the approach you take, each excluding lines that include the targeted phrases, syntax, words, or text match. For a practical example that advanced Mac users may find helpful, we can use grep exclusion when to find defaults matches, but excluding some selected defaults strings from the output. In the example here well print command history for defaults string matches, but exclude anything matching having to do with iTunes as defined by com.apple.itunes: So if youve been following along, this would report back all historical executions of the defaults write command, but excluding anything that pertained to the iTunes application. Nice huh? If you have any particularly handy uses of excluding matches with grep, share them with us in the comments below! And if you enjoyed this article, youd almost certainly like where there is plenty more to learn!
No comments