Discover the Power of Grep
The simplest solutions are often the most powerful. Such is the case with grep, a small but mighty Linux command-line utility. It's incredibly useful when you want to search the contents of your files, and it does the job quickly and accurately. Its strength lies in the multitude of options which can be combined to suit your needs. Another advantage of grep is its portability – it works on any Linux distribution, and all you need to run it is a console. Even if you're a beginner, you won't have problems using grep, and if you're still afraid of using the command line, you won't be after reading this handy guide.
Grep is usually installed by default, but if your Linux distribution didn't come with it, you can search for grep in the repositories and install it using your preferred package manager. The basic functionality of the program is straightforward – it reads the contents of human-readable files and outputs the result which corresponds to your search request. The syntax is simple and looks like this:
grep -option “search string” filename
Options are entered in the form of a switch, and the search string is the actual text (a character, word or phrase) that you're looking for. Note that it doesn't have to be enclosed in quotation marks. You will also have to provide the name of the file, or the path to a folder in which you want grep to look for your string.
In the first example we'll use grep to see if the selected file contains a particular word:
grep word filename
As you can see, there are two lines in this file containing the word “python”. If we want the search to be case insensitive, we should add the -i option right after grep:
grep -i python filename
The output would show lines containing “python”, but also “Python”, “pYthOn” or any other possible variation. If we wanted to find a phrase, we would use quotation marks. For example, I often use grep to check if I have an album in my music catalogue (which is in .csv format):
grep -i “Sound of the Universe” music.csv
The results show that I indeed have this Depeche Mode record, but it's saved in the catalogue as “Sound Of The Universe”. Not a problem for grep, because I used the -i switch, so the search string is recognized and processed correctly.
Let's say you want to count how many times you used the definite article in a text. If you searched for “the” or -i “the”, the results would include all words which contain that character sequence (such as thesaurus, Othello or breathe). However, if you add the -w switch, which stands for word, grep will only show instances where “the” is a separate word. To find out how many times you wrote “the”, just add the -c (count) option:
grep -i -w -c “the” mytext.doc
The options need not be written separately, so the results would be the same if you wrote grep -iwc “the” mytext.doc. Still, be careful, because -c and -C are not the same. The latter can also be written as --context, and it will show you the context of your search string in a file. You can specify the number of surrounding lines after the -C switch:
grep -i -C 5 “test” mytext.doc
Grep can operate on the level of folders in the sense that it can process multiple files at once, regardless of the file format. All you have to do is write their names separated by spaces. If the files are not in the same folder, you'll have to write their full path before the filename.
grep -i “Black Celebration” music.csv unsorted.csv list.txt playlist.log
Grep will format the results so that the name of each file is written before the lines containing your search string. This way you'll know what's where.
When searching within files of the same format, you can use the asterisk (*) as a wildcard:
grep -i “sample search” *.txt
This example query will try to find the search string in every TXT file in that folder.
Grep also supports recursive search, meaning that it can automatically look in all subfolders of a specified folder. The switch to activate this option is -r, and the asterisk stands for the active folder (the one you're currently working in).
If you know that some files in a folder contain a specific word, but don't know which ones, you can use grep to list their filenames:
grep -i -l word *
Uppercase (-L) will do the opposite – it will show you which files don't have the word you're looking for.
Advanced users will appreciate grep's ability to work with regular expressions. To learn more about this and many other grep features, simply type man grep into your console. You might also be interested in zgrep and bzgrep – variations of grep which can search through .gz and .bz files, respectively.
If you're a Windows user, do not despair - you too can harness the power of grep by using one of its GUI implementations, such as Agent Ransack, Windows Grep or AstroGrep. Linux users looking for a graphical frontend for grep might be satisfied with Recoll or SearchMonkey. Both are practical and user-friendly, but nothing beats grep when it comes to speed. Plus, you can use grep to impress your friends with your awesome command-line skills :).
Ivana Isadora Devcic is a freelance writer, copyeditor and translator fluent in English, Swedish, Croatian and Norwegian. She's a Linux user and KDE fan interested in web design, productivity and personal branding. Ivana tweets about the world around her as @skadinna.
Reader Comments