awk command to search keyword or strings in file

In this tutorial we will learn, awk command to search keyword or strings in file . We will search single as well as multiple keyword with awk Command. This is a commonly used tips by most of the System Admin. Whereas the same result you can also find by using grep command.

Introduction of AWK Command

AWK is an interpreted programming language designed for text processing and typically used as a data extraction and reporting tool.AWK was created at Bell Labs in the 1970s and its name is derived from the family names of its authors – Alfred Aho, Peter Weinberger, and Brian Kernighan. (Reference : Wikipedia)

Before directly moving to steps part, have a look on file contents which we are using for this practical.

File Name : employee.list

linux@tuxworld:/tmp$ cat employee.list 
Emp-ID	Name	Designation	Dept.		Ext.
12	Joe	Manager		HR		2345
67	George	Developer	IT		8467
100	Ravi	Sysadmin	IT		9901
3789	Tina	Manager		Security	9948
1200	Justin	Supervisor	Fascility	1095
456	Katie	Executive	HR		6660
linux@tuxworld:/tmp$

Search single keyword with awk command

To search single keyword by using awk command, use below given syntax

awk '/Keyword/' /path/file-name

Example: Here we are searching keyword George and HR respectively.

linux@tuxworld:~$ awk '/George/' /tmp/employee.list 
67	George	Developer	IT		8467
linux@tuxworld:~$ 
linux@tuxworld:~$ awk '/HR/' /tmp/employee.list 
12	Joe	Manager		HR		2345
456	Katie	Executive	HR		6660
linux@tuxworld:~$ 

Search multiple keyword with awk command

To search multiple keyword by using awk command, use below given syntax

awk '/Keyword-1|Keyword-2|Keyword-3|Keyword-N/' ' /path/file-name

Example: We will search 3 Keywords in single awk command.

Keyword-1 = Ravi
Keyword-2 = Joe
Keyword-3 = Tina

linux@tuxworld:~$ awk '/Ravi|Joe|Tina/' /tmp/employee.list 
12	Joe	Manager		HR		2345
100	Ravi	Sysadmin	IT		9901
3789	Tina	Manager		Security	9948
linux@tuxworld:~$ 
linux@tuxworld:~$

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.