ACAD – grep

By Ninad Pundalik

Imagine this… You have just created a Web site with about a hundred different HTML and PHP files. There is a particular string that has to be replaced, and you don’t remember which file it was in, or where it was in the file. What do you do at such a time? Make the computer do all the hard work, and you do the smart work. Here’s a solution:

Today, we have a look at one of the most basic tools that Linux users know and utilise in their vast programming efforts. ‘grep’ (Global Regular Expression Print) is a command used to search for and print lines in a text file that match the particular string given to them. What you just saw above was one of the most common uses of grep. The output of one command is piped to grep, which then searches and prints the lines containing the given search string.

The manpage of grep gives the syntax for grep as ‘grep options pattern file’. The useful options for grep are as follows:

  • -e PATTERN . Tells grep to assume PATTERN as a regular expression.
  • -i . It makes grep case insensitive.
  • -v . It inverts/negates the selection due to PATTERN.
  • -c . Print the count of matching lines.
  • -H . Print the filename along with the line number.
  • -h . Do not print filename
  • -R or -r . Read directories recursively. This is done when file given is a directory.

My favourite use of grep is the following:

As you would have realised, I come to know if a particular process is running or not on my system. Hope you will find this tool useful. Tomorrow, I’ll take up the basic packages that have to be installed on an Ubuntu system after a clean install of the system.