grep

Introduction

This is an article about grep. You can read about SED in the article «Basics of SED» about AWK - in the article «AWK»

The flag -v lets you invert the result. That is, to find the opposite of what grep without this flag would search for

-v, --invert-match
       Invert the sense of matching, to select non-matching lines.

By default I assume that you are working in Bash on Windows 10 or in Bash on Linux .

Basic grep commands

Output all mentions of a word

Suppose you have launched CentOS Linux and want to see all installed packages whose name contains the word kernel

yum list installed | grep kernel

abrt-addon-kerneloops.x86_64 2.1.11-60.el7.centos @base kernel.x86_64 3.10.0-1160.el7 @anaconda kernel.x86_64 3.10.0-1160.2.2.el7 @updates kernel.x86_64 3.10.0-1160.6.1.el7 @updates kernel-devel.x86_64 3.10.0-1160.2.2.el7 @updates kernel-devel.x86_64 3.10.0-1160.6.1.el7 @updates kernel-headers.x86_64 3.10.0-1160.6.1.el7 @updates kernel-tools.x86_64 3.10.0-1160.6.1.el7 @updates kernel-tools-libs.x86_64 3.10.0-1160.6.1.el7 @updates

And conversely, you can view all lines where there is no word kernel : you need to add the -v option

yum list installed | grep -v kernel

If you need to find something in a file, instead of | you can use the expression

grep '\bkernel\b' huge_file

Here huge_file is the name of a file in the current directory in which we look for the standalone words kernel.

That is, the words akernel or kernelz will not be found

Output everything that starts with a word

If we now do not need the packages in which the word kernel is in the middle, but only those that start with kernel , so let us add the ^ sign before the word

yum list installed | grep ^kernel

kernel.x86_64 3.10.0-1160.el7 @anaconda kernel.x86_64 3.10.0-1160.2.2.el7 @updates kernel.x86_64 3.10.0-1160.6.1.el7 @updates kernel-devel.x86_64 3.10.0-1160.2.2.el7 @updates kernel-devel.x86_64 3.10.0-1160.6.1.el7 @updates kernel-headers.x86_64 3.10.0-1160.6.1.el7 @updates kernel-tools.x86_64 3.10.0-1160.6.1.el7 @updates kernel-tools-libs.x86_64 3.10.0-1160.6.1.el7 @updates

Output everything that ends with a certain set of characters

grep -E 'ion$' huge_file

compensation
generation

Find words by their first and last letters

Suppose you know only the beginning and the end of a word

grep -E '^to..le$' huge_file

topbicycle

Several characters in a row

Find words with five vowels in a row

grep -E '[aeiou]{5}' /usr/share/dict/words

cadiueio Chaouia cooeeing euouae Guauaenok miaoued miaouing Pauiie queueing

Search in the output of a function

You first need to call the function with all the flags, then put a pipe and run grep

For example, from the file today.log you can extract all lines with errors

cat today.log | grep ERROR

Search in the output of a function by several words

For example, you need to pick several processes out of top at once:

top | grep 'process1\|process2\|process3'

How many times a word is used in the text

Let us start with a simple example: the word occurs in a line only once.

For example, the server operation log contains lines of the form

2026-11-17-11-11-10-ERROR: Something is NOK

You need to calculate how many errors are recorded in the log file 2026-11-17-log.txt

grep -o -i ERROR 2026-11-17-log.txt | wc -l

If you want to use this command in a script - I recommend the article Output of a command into a variable in a bash script

Find empty/non-empty lines

Find all empty lines in the file ntp.conf

grep '^$' /etc/ntp.conf

Find all non-empty lines in the file ntp.conf

grep -v '^$' /etc/ntp.conf

grep -E

The regular grep cannot cope with some tasks, so an extended mode is needed.

Find in the file file all foobar or foo bar with exactly one space

grep -E 'foo\s?bar' file

Find in the file file all foobar or foo bar with exactly two spaces

grep -E 'foo\s{2}bar' file

A more complex example. The employees of TopBicycle need to figure out which bicycles in the list are missing or have an incorrectly written serial number.

The number must be in the format 111-11-1111, that is three digits hyphen two digits hyphen four digits

cat bikes.txt

Stels,Pilot,111-22-3333
Merida,BigNine,,
Stark,Cobra,xxx-xx-xxx
Forward,Tracer,1234-0
Author,Grand,444-55-6666
Stels,Pilot21,111-22-3344
Giant,Lannister,555-66-7777
Helkama,Jopo,,

grep -vE '\b[0-9]{3}-[0-9]{2}-[0-9]{4}\b' bikes.txt

Merida,BigNine,,
Stark,Cobra,xxx-xx-xxx
Forward,Tracer,1234-0
Helkama,Jopo,,

Correct entries

grep -E '\b[0-9]{3}-[0-9]{2}-[0-9]{4}\b' bikes.txt

Stels,Pilot,111-22-3333
Author,Grand,444-55-6666
Stels,Pilot21,111-22-3344
Giant,Lannister,555-66-7777

Article author: Andrei Olegovich

Banner image