Blog / Using the Grep command on Linux/UNIX systems

Using the Grep command on Linux/UNIX systems

by SW Team

The Grep command, which stands for "global search for lines matching a regular expression", is used to search for and find text patterns in files containing regular expressions. Moreover, this command comes pre-installed in all Linux distributions. In this guide, we will explore the most frequent uses of the grep command, as well as some popular use cases.

cta:cloud_so

Linux grep command

The grep command is useful to search for a regular expression or string in a text file. To illustrate this, we will create a text file called swhosting.txt and add content for testing:

SWHosting is a leading provider of web hosting services.
They offer reliable, high performance solutions for individuals and businesses.
With an excellent track record of service and satisfaction, SWHosting is a trusted name in the industry. Their state-of-the-art infrastructure, secure data centres and optimised performance make them the perfect choice for your hosting needs.

Perfect. Now we're ready to run some grep commands and manipulate the output to get the results we're looking for. If you want to search for a string in a file, simply run the command with the following Syntax:

$ grep "character" filename 
ó 
$ filename grep "character"

Ejemplo:

$ grep "SWHosting" swhosting.txt

** Exit:** grep

As you can see, grep has not only searched for and found the string "SWHosting", but also printed out the lines where the string appears. If the file is located in a different file path, be sure to specify the file path as follows:

grep "character " /path/to/file

Colouring Grep results with the option --color

If you are working on a system that does not display the search string or pattern in a different colour than the rest of the text, use --color to highlight the results, as we have seen in the first example where we could see the string in a different colour than the rest of the text.

Ejemplo:

$ grep --color "data" swhosting.txt 

Exit: grep

Search for a string recursively in all directories

If you want to search for a string in your current directory and all other subdirectories, you can search using the "-r" parameter as follows:

$ grep -r "SWHosting" *

grep

cta:domains

Ignoring case-sensitivity

In the example above, our search results gave us what we wanted because the string "SWHosting" was specified exactly with its respective uppercase letters. If we try to search for the string in lower case, we will find that there is no output. This is because "grep" cannot find and match the string "swhosting", since the first letters are Lowercase. To ignore the case distinction, we can make use of the -i parameter.

Example:

$ grep "swhosting" swhosting.txt
$ grep -i "swhosting" swhosting.txt

Exit: grep

Surprising, isn't it? The -i is commonly used to represent case-insensitive strings.

Count the lines on which strings match with the -c option

To determine the total number of lines on which the string pattern is found or resides, run the following command:

$ grep -c "SWHosting" swhosting.txt

Exit:

grep

Using Grep to invert the output

To change the output of Grep, you can use the -v option. When using this option, grep will invert its output, i.e. instead of printing matching lines, it will print all lines that do not match the expression.

$ grep -v "SWHosting" swhosting.txt

Exit:

grep

It can be seen that grep has displayed the lines that do not include the search pattern.

Number the lines containing the search pattern with the -n option

To number the lines containing the string pattern, use the -n option as follows.

$ grep -n "SWHosting" swhosting.txt

Exit:

grep

Search for the exact word using the -w option

With this parameter, we can search for an exact word. Next we will see how we search for the string "Hosting" without this parameter and we obtain a result, on the other hand, when we search with the parameter -w, we will not obtain any result.

Output:

grep

Using pipes with grep

The grep command can be used in combination with "pipes" to get a different result. For example, if you want to check if a specific package is installed on your Ubuntu system, you can run:

$ dpkg -l | grep "package-name"

Example:

dpkg -l | grep "apache" **Example:** ** Output:** dpkg -l | grep "apache"

Output:

grep

Using grep with REGEX - regular expressions

The concept of REGEX is an abbreviation for REGular EXpression. A REGEX is a string of characters used to find a pattern. The following are some examples:

  1. ^ Matches characters at the beginning of a line. 2.

  2. $ Matches characters at the end of a line.

  3. "." Matches any character

  4. [a-z] Matches any character between A and Z

  5. [^ ..] Matches anything that is not enclosed in square brackets

Example:

$ grep ^S swhosting.txt

Output:

grep

Getting help with more Grep options

If you need to know more about using the Grep command, run the following command to preview other parameters or options you can use in conjunction with the command.

$ grep --help

Output: !

grep

Conclusion

In summary, the grep command in Linux is a powerful and versatile tool that can significantly improve your hosting experience. With its efficient search and filtering capabilities, you will be able to quickly find the information you need and optimize the performance of your server. Take full advantage of this tool and take your hosting to the next level!

cta:cloud_so

i