Grep is very powerful unix command line tools to search plain-text dataset using regex. The basic functionality of grep is as follow:
grep [pattern] [file]
Since wikipedia already has an excellent introduction for the basic functionality. I will jump out to uncommon feature to search from list of words that I found out yesterday because I need to clean up and analyse some csv files.
Let say we have this text file where we want to search for something.
apple Pineapple banana pear PEACH orange
Then we have another list words where we want to grep from.
apple orange watermelon peach
grep -f lookup.txt fruitlist.txt yields
apple Pineapple orange
-f command line arguments is used to pass one or more separated pattern from
To search the exact word add
-w argument before
-w means the expression in the list is searched as a word.
grep -wf lookup.txt fruitlist.txt yields
Pineapple is get rid.
To invert the search to find string not in the lookup file, use
grep -vwf lookup.txt fruitlist.txt yields
Pineapple banana pear PEACH
To search by ignoring case, add
grep -iwf lookup.txt fruitlist.txt yields
apple PEACH orange
That's it example for me. Indeed
man grep is not a pleasant documentation, but we can get the whole feature explanation there.