How to Grep for Strings in Files

Linux and UNIX-like systems, all system configuration information is stored and manipulated in plain text form. The grep tool allows searching for a text/string, in a file or output of the command, for a term or pattern matching regular expressions. ie Grep is used to search text/string for patterns specified by the user.

When grep finds match in a line, it copies into the screen that is stdout. Using grep command you can replace the string in given file. Grep provides a number of additional options that, if specified , force the program to output the context for each match.

Syntax

grep [ option(s) ] pattern [file(s) ]

Basic Grep Command

The basic usage of grep command is to search for a specific string in the specified file as shown below.

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

Above example grep searches the /etc/passwd file for root string and redirected to stdout.

Options

-i searches for the given string/pattern case-insensitively. So it matches all the words such as “hal”, “HAL” case insensitively as shown below.

Even you can use Line and, anchors, character classes, wild cards,

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

Searching string in a given file

Use below grep command to search specific string in a file

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

Searching string in multiple files

Use below grep command to search specific string from multiple files

# 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

Regular Expressions in files

? The preceding item is optional and matched at most once.
• * The preceding item will be matched zero or more times.
• + The preceding item will be matched one or more times.
• {n} The preceding item is matched exactly n times.
• {n,} The preceding item is matched n or more times.
• {,m} The preceding item is matched at most m times.
• {n,m} The preceding item is matched at least n times, but not more than m times.

# grep " *oot" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

Searching full word using grep –w

The -w 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:

Grep with option -A

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

-B option which displays the N lines before the string match

When the string matches it will 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

-E option can be used search multiple strings

-e is strictly the flag for indicating the pattern you want to match against. -E controls whether you need to escape certain special characters

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

Or we can use

# ls | grep ‘li\|ovo’
linux24
ovo8_linux24.sh
#

Exclude specific file from searching

Search string in all files except syslog.log

# grep -r string * | grep -v ‘/\syslog.log/’

Conclusion

In this tutorial we learned how to search for strings in files using grep method. I hope your enjoyed reading this and please leave your suggestions in the below comment section.

Bobbin Zachariah 7:25 pm

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.