6 May 2012
3 mins read

How to Use Grep Command to Find Text in Files

Linux and UNIX-like systems, all system configuration information is stored and manipulated in plain text form. So searching text would come very common task for editing and log analysis.

The grep command allows searching for a text or string in a file or from output console of a command, for a term or pattern matching regular expressions. When grep finds match in a line, it copies results into the screen ie stdout.

In this tutorial we learn how to use grep command with practical examples.

Grep Command 

Lets check grep command syntax before we start explaining its usage.

grep [OPTIONS] PATTERN [FILE...]

We will discuss in detail most common grep options and patterns we can use for searching.

How search a string in a file

In the below example grep searches the /etc/passwd file for the string ‘root’ and redirected output to stdout.

$ grep "root" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin

Search text from multiple files

You can search specific text from multiple files using the following command:

$ grep "root" /etc/passwd /etc/group /etc/passwd:root:x:0:0:root:/root:/bin/bash /etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin /etc/group:root:x:0:root /etc/group:bin:x:1:root,bin,daemon /etc/group:daemon:x:2:root,bin,daemon /etc/group:sys:x:3:root,bin,adm /etc/group:adm:x:4:root,adm,daemon /etc/group:disk:x:6:root /etc/group:wheel:x:10:root

Grep case insensitive search

Options -i searches for the given string/pattern case insensitively.

In following examples grep matches all the words such as “hal”, “HAL” case insensitively:

$ grep -i HAL /etc/passwd halt:x:7:0:halt:/sbin:/sbin/halt haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

Recursive Search

Recursive search is achieved by using -r option that will search Recursively all file skipping  symbolic links. If you want to include symlinks use -R.

In the following example, grep command will search for the string ‘ngnix’ in all files inside /var directory.

$ grep -r nginx /var

Search for a String in Command Output (stdout)

You can use pipe the output for a command and grep for the pattern from its output.

In the following example I am searching for a file with name ‘backup’ using the ls command

$ ls | grep backup

Here i am doing multiple pipe and searching for the ‘docker’ and ‘apache’ process from ps command.

$ ps -ef | grep docker | grep apache

Regular Expressions in files

Regular Expressions is a pattern to match for each input line. Basic and Extended are the two regular expression used by grep command.

Basic regular expression

By default, grep interprets the pattern as a basic regular expression.

The ^ (caret) symbol – The pattern following it must occur at the beginning of each line. In the following example, the string ^welcome will match only if it occurs at the very beginning of a line.

$ grep “^welcome” filename

Extended Regular Expression

Use -E ( or –extended-regexp) option if you want grep to understand it as an extended regular expression.

The following command will extract all links from an file

$ grep –Eoi '<a[^>]+>.*</a>' filename

o : By default, grep prints entire line which contains the search pattern. This option instructs grep command to print only the matching words instead of entire line.

i : This option ask grep command to ignore the case while matching the pattern.

Search a full word using grep

The -w (word-regexp) flag for grep will make the given expression match only whole words.

$ grep -iw "samba" /etc/samba/smb.conf # This is the main Samba configuration file. You should read the # here. Samba has a huge number of configurable options (perhaps too # For a step to step guide on installing, configuring and using samba, # read the Samba-HOWTO-Collection. This may be obtained from:

Display lines after the string match

The -A option which displays the N lines after the string match.

$ grep -A 2 "root" /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

Display lines before the string match

We can use -B option to display N lines before the string for a given file

$ grep -B 2 "root" /etc/passwd root:x:0:0:root:/root:/bin/bash -- news:x:9:13:news:/etc/news: uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin

Search multiple strings

We can search multiple patterns or strings using OR operator | and |.  -E  (extended-regexp) controls whether you need to escape certain special characters. -e is strictly the flag for indicating the pattern you want to match against.

Example 1: using -E option

$ ls | grep -E “li|ovo” linux24 ovo8_linux24.sh

Or

$ grep -E "foo|bar" *.txt

Example 2: without -E

$ ls | grep ‘li|ovo’ linux24 ovo8_linux24.sh

Or

$ grep "foo|soul|bar" *.txt

Exclude specific file from searching

You can use -v option to ignore searching. The following command will search the string ‘error’ in all files except ‘syslog.log’

$ grep -r error * | grep -v ‘/syslog.log/’

Conclusion

In this tutorial we learned how to use grep command to find text in files. I hope your enjoyed reading this and please leave your suggestions in the below comment section.

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is the editor-in-chief of Linoxide and has an experienced team of Linux enthusiastic authors who makes this blog awesome. Linoxide is one of the top 20 Linux Blog by whizlabs.

Leave a Reply

Your email address will not be published.

Previous Story

Find Detailed NFS Mount Options in Linux

Next Story

How to Display Linux Hardware Info via Command Line

Latest from Blog

Top 8 Reasons to Use Garuda Linux

Have you been going back and forth between multiple Linux flavors in search of an exciting experience? Or perhaps you are coming from a Windows or MAC environment and want to try

How to Rename Multiple Files in Linux

In a Linux system, you can easily rename a file using mv command. But, if you have multiple files which you want to rename, in this situation you need some extra tools

How to Install TensorFlow on Ubuntu 20.04

Tensorflow is an open-source platform for machine learning and artificial intelligence. It is developed by the Google Brain team. It contains tools, libraries, and community resources for developers to build ML powered
Go toTop