Image for post
Image for post
Photo by Romain Vignes on Unsplash

grep is powerful, using its regular expressions you can do anything it terms of matching in a single line. Lets see some examples.

starting with grep and its options

grep follows the following format

$ grep [options] regex [file(s)]

options:

  • i: case insensitive.
  • v: invert matching results.
  • c: print the number of matches or non-matches if -v is used.
  • l: print the filename of match instead of the line matched text.
  • L: print the filenames that didnt matched text.
  • n: print the line number of the matched text.

generate files with some data to work with grep

lets generate a file with some strings to work with our grep examples

$ ls /bin/ > ~/test_1.txt

The above command will generate a listing of all files inside /bin and will redirect the output at your home directory in a file named test_1.txt

echo "this is a test file" > ~/test_2.txt

The above command created a file named test_2.txt with the text “this is a test file”

Example 1: simple text search

We have to check if text ‘zip’ exists in files test_1.txt or test_2.txt, to do this we can run the following:

$ grep zip test_1.txt test_2.txt

output:

test_1.txt:bunzip2
test_1.txt:bzip2
test_1.txt:bzip2recover
test_1.txt:funzip
test_1.txt:gpg-zip
test_1.txt:gunzip
test_1.txt:gzip
test_1.txt:mzip
test_1.txt:p7zip
test_1.txt:preunzip
test_1.txt:prezip
test_1.txt:prezip-bin
test_1.txt:unzip
test_1.txt:unzipsfx
test_1.txt:zip
test_1.txt:zipcloak
test_1.txt:zipdetails
test_1.txt:zipgrep
test_1.txt:zipinfo
test_1.txt:zipnote
test_1.txt:zipsplit

We can see the occurances with lines that contain text ‘zip’ in file test_1.txt and none occurance in file test_2.txt

Example 2: Print the count of occurances

Many times we dont need the full output, we just need to know how many times a match exists, to do this we can run:

$ grep -c zip test_*txt

-c: this option tells grep to count number of occurances.

output:

test_1.txt:21
test_2.txt:0

We can see that text ‘zip’ exists 21 times in file test_1.txt and 0 times on test_2.txt.

Note: on your computer the count of occurances might vary on file test_1.txt

Example 3: Print only file names that have occurances

To get only files with occurances we can run:

$ grep -l zip test_*txt
  • l: this option tells grep to print only files that have the text ‘zip’

output:

test_1.txt

Which is the only file that contains the text ‘zip’ inside.

Example 4: Print only file names that dont have occurances

To get only files without any occurances of text ‘zip’ we can run:

$ grep -L zip test_*txt
  • L: this option tells grep to print only files that dont match with a search

output:

test_2.txt

Regular Expressions

You might not know it, but you have used regular expressions to do the search.

Searching for text ‘zip’ you have asked grep to do the following:

  1. Search each line of files for a sequence of characters ‘zip’.
  2. The characters can start everywhere in the line.
  3. The characters can end everywhere in the line.

But regular expressions can be even more powerfull and produce even more narrow results based on our needs.

Example 5: Find lines that start with the text ‘zip’

To get only lines that start with the text ‘zip’ we can use the ‘^’ metacharacter.

$ grep ^zip test_1.txt

output:

zip
zipcloak
zipdetails
zipgrep
zipinfo
zipnote
zipsplit

We can see that all lines start with text ‘zip’.

Example 6: Find all lines that do not start with text ‘zip’

To get all lines that do not start with text ‘zip’ we can use the -v option which inverts results.

$ grep -v ^zip test_1.txt | tail -n 20

the tail command here is used to limit the number of output to 20 lines.

output:

xzmore
yacc
yaml2obj-6.0
yaml-bench-6.0
yelp
yes
ypdomainname
zcat
zcmp
zdiff
zdump
zegrep
zenity
zfgrep
zforce
zgrep
zjsdecode
zless
zmore
znew

We can see that there is no line that starts with ‘zip’.

Example 7: find all lines that end with text ‘cat’

To get all lines that end with cat, we can use the ‘$’ metacharacter.

$ grep cat$ test_1.txt

output:

bzcat
cat
fc-cat
gencat
gvfs-cat
lz4cat
lzcat
mcat
netcat
ntfscat
pacat
precat
systemd-cat
xzcat
zcat

We can see that all lines end with ‘cat’ text.

End of part 1

This was the first part of my grep tutorial, i hope you found it very easy to start and you dont afraid to use grep, grep is powerfull and can be difficult to master, but its not so difficult todo simple things that can be the 70% of day to day work.

Written by

DevOps engineer, loves Linux, Python, cats and Amiga computers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store