Grep Command Examples in Unix/Linux Shell Scripting

thelinuxterminal

Author - Sanjay

Introduction

For most of the developers and admin , manipulating something after reading a particular file is a common task that most of us hate to do . Grep Command is mostly used in these cases to work with while searching in any kind of text. Grep is Powerful text bases regular expression search utility present in most of linux falvours. In this article we will understand and learn few examples which we use in our shell scripts.

Pre-Requisistes

A CentOS/RHEL 7 production server or a Linux VPS .

Installing Grep in Linux/Unix

In most of the cases grep is pre installed in most of the linux distros. But it is possible in some of the linux distros grep might not be present . Have a look how we can install grep .

Installing grep in Ubuntu Distros

sudo apt-get install grep

In RedHat linux Distros yu can install by simply

yum install grep

You can now verify the grep installation by simply typing

grep ---version

Common Grep Command Examples in Linux

The grep command is the one of the most commonly used Unix utility for text based searching in files or any other types . It accepts regular expressions, and can produce output in various formats.

Lets have a look athe various options available in grep command using examples.

How to Search for Lines with a specific pattern

$ grep pattern filename

Lets say i want to find a text name as John in my text file .

grep "John" terminal.txt

This would list down all the lines with the word "John" in my text fle.You can also search in multiple files using the same patttern above

grep "John" file1.txt file2.xt file3.txt 

Displaying received data on an Network interface

Using just the ifconfig eth0 command, a heap of data can be printed to the screen. To show just the packets received, we can isolate the lines that contain RX packets (RX for received). This is where grep comes in: We can enter ifconfig command to list network interfaces and execute below command to list packets .

4bits$ ifconfig eth0 | grep "RX packets"

Get User Account Data using Grep

The local user account database in Linux is the /etc/passwd file and this is readable by all user accounts. If we want to search for the line that contains our own data, we can use either our own login name in the search or use parameter expansion and the $USER variable. We can see this is the following command example:

4bits$ grep "$USER" /etc/passwd

Grep Command to List Number of CPU Cores

If we want to list the number of CPUs in a system . We can get the contents of cpuinfo file and grep that output .

4bits$ grep -c name /proc/cpuinfo

Grep Command to search Multiple Patterns with in single line

The grep command can also be used to match multiple patterns at once. Simply separate the patterns with a pipe character ("|"). For example, to find all lines that contain either "frugalis" or "john", you could use the following command:

grep -i 'frugalis|john' file.txt

4bits@192  % grep -E 'thelinuxterminal|tez' test.txt
thelinuxterminal
4bits@192  % 

grep pattern

Finally, the grep command can be used to match patterns that are not necessarily whole words. By default, grep matches patterns that are surrounded by word boundaries (such as spaces or punctuation marks). The -w flag tells grep to match only whole words.