OSETC TECH

How to Grep a whole Word in a File in Linux

This post will guide you how to grep a word from a text file using grep command under Linux opearting systems. How do I find specific word in a file on your Linux system.

Grep Command


grep searches for PATTERN in each FILE. A FILE of “-” stands for standard input. If no FILE is given, recursive searches examine the working directory, and nonrecursive searches read standard input. By default, grep prints the matching lines.

The syntax of the Grep command are as followss:

grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] -e PATTERN ... [FILE...]
grep [OPTIONS] -f FILE ... [FILE...]

Options:

-i, --ignore-case Ignore case distinctions, so that characters that differ only in case match each other.
-w, --word-regexp Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.

See Alos: More Grep Command Examples

Find Command


GNU find searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name. If no starting-point is specified, `.’ is assumed.

The syntax of the Find command is as follows:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]

Options:

-name pattern Base of file name (the path with the leading directories removed) matches shell pattern pattern.
-exec command {} This variant of the -exec action runs the specified command on the selected files,

See Also: More Find Command Examples

Ack Command


ack is designed as an alternative to grep for programmers. ack searches the named input FILEs or DIRECTORYs for lines containing a match to the given PATTERN. By default, ack prints the matching lines. If no FILE or DIRECTORY is given, the current directory will be searched.

The syntax of the Ack command are as followss:

ack [options] PATTERN [FILE...]
ack -f [options] [DIRECTORY...]

Find a Specific Word in a File Using Grep Command


If you want to find a word (root)from a file /etc/passwd , and you can use the following command:

$ grep -w "root" /etc/passwd

outputs:

[devops@mydevops ~]$ sudo grep -w "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

This command will print all lines that containing word “root” from the given file /etc/passwd.

If you want to ignore case to find a word (word or Word or WORD) from file /etc/passwd, and you can pass the -i option to the grep command, type:

$ grep -i -w "root" /etc/passwd

Find a Specific Word in Multiple Files Using Find Command


If you want to find a specific word from mutliple files (*.conf) located /etc directory, and you can use find command in combination with grep command, type:

$ sudo find /etc -name "*.conf" -exec grep -w "root" {} \;

Outputs:

[devops@mydevops ~]$ sudo find /etc -name "*.conf" -exec grep -w "root" {} \;
if [[ $username != "root" ]]; then
<policy user="root">
<policy user="root">
<policy user="root">
<policy user="root">
<!-- Only root can own the service -->
<policy user="root">
<policy user="root">
<policy user="root">
<!-- Allow root to own the name on the bus -->
<!-- Only root can own the service -->
<policy user="root">
<policy user="root">
<policy user="root">
<!-- Only root can own the service -->
<policy user="root">
<policy user="root">
<policy user="root">
<!-- Only root can own the service -->
<policy user="root">
<!-- Only root can own the service -->
<policy user="root">

Note: the -exec option will be used to pass the found files to grep command for searching a whole word.

Find a Specific Word in a File Using Ack Command


You can also use another command called Ack to find a word from a given file. This command is not installed by defaut, so you need to install it by running the following command:

$ sudo yum install ack

Outputs:

[devops@mydevops ~]$ sudo yum install ack
Repository google-chrome is listed more than once in the configuration
CentOS-8 - AppStream 1.7 kB/s | 4.3 kB 00:02
CentOS-8 - Base 1.5 kB/s | 3.8 kB 00:02
CentOS-8 - Extras 597 B/s | 1.5 kB 00:02
google-chrome-x86_64 1.2 kB/s | 1.3 kB 00:01
Extra Packages for Enterprise Linux 8 - x86_64 3.1 kB/s | 5.5 kB 00:01
Dependencies resolved.
====================================================================================================================================================================================
Package Arch Version Repository Size
====================================================================================================================================================================================
Installing:
ack noarch 3.0.3-1.el8 epel 81 k
Installing dependencies:
perl-version x86_64 6:0.99.24-1.el8 AppStream 67 k
perl-File-Next noarch 1.16-10.el8 epel 25 k

Transaction Summary
====================================================================================================================================================================================
Install 3 Packages

Total download size: 173 k
Installed size: 338 k
Is this ok [y/N]: y
Downloading Packages:
(1/3): perl-version-0.99.24-1.el8.x86_64.rpm 336 kB/s | 67 kB 00:00
(2/3): perl-File-Next-1.16-10.el8.noarch.rpm 119 kB/s | 25 kB 00:00
(3/3): ack-3.0.3-1.el8.noarch.rpm 331 kB/s | 81 kB 00:00
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 52 kB/s | 173 kB 00:03
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : perl-File-Next-1.16-10.el8.noarch 1/3
Installing : perl-version-6:0.99.24-1.el8.x86_64 2/3
Installing : ack-3.0.3-1.el8.noarch 3/3
Running scriptlet: ack-3.0.3-1.el8.noarch 3/3
Verifying : perl-version-6:0.99.24-1.el8.x86_64 1/3
Verifying : ack-3.0.3-1.el8.noarch 2/3
Verifying : perl-File-Next-1.16-10.el8.noarch 3/3

Installed:
ack-3.0.3-1.el8.noarch perl-version-6:0.99.24-1.el8.x86_64 perl-File-Next-1.16-10.el8.noarch

Complete!

Once Ack package is installed successfully on your Linux system, and you can try to use this command to find a whole word from /etc/passwd file, type:

$ ack "root" /etc/passwd

Outputs:

[devops@mydevops ~]$ ack "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

Conclusion


You should know that how to grep a whole word from a file using grep/find/ack commands in your CentOS/RHEL/Ubuntu Linux.