Best Laptop
UNIX Grep command examples

UNIX Grep command examples

Hi Friends, today we will see some use of UNIX GREP command examples which is very useful and powerful command. As we all know that grep command is used mainly for pattern search in files. But this is just the basic explanation of grep command. Today we will see how grep is enriched with a lot of options to do pattern search in many other ways.

Let’s consider below example file on which we will be performing our operations. The name of the file is test_grep. It has 6 records currently.

pen
pencil
book
chair
table
cup

Lets now start our tutorial by running different UNIX grep command examples on this file.

1) Grep Pattern search

This is the most basic of UNIX grep command examples. We are trying to output those rows which contains pattern book. Below is the output which is very obvious (Please note that the pattern can be written with in single or double quotes also. Output will be same)

UNIX GREP COMMAND EXAMPLES

Next try to search pattern pen. Hereyou will get both the values: pen and pencil. Since pencil also contains pattern pen in it. So, what if you just want pen to be shown in output? Check point 2:

UNIX GREP COMMAND EXAMPLES

2) Exact pattern search

If you want to output those records which contain only the mentioned pattern, means no extra characters. We have to use below command. Using -w option will do the exact pattern match

UNIX GREP COMMAND EXAMPLES

3) Case-insensitive match

By default, grep obeys the case sensitivity. It outputs only those patterns which matches the case used in command. Let’s see using this example. Consider we modified our input file and added another record at the end: Pen (here P is in upper case. First pen has small p)

If you run below command, you only get smaller case p output.

UNIX GREP COMMAND EXAMPLES

But if you want both the records having Pen/pen irrespective of their case, then run below command. Using -I will ignore the case. Sequence of -w/-i can be interchanged.

UNIX GREP COMMAND EXAMPLES

4) Match pattern which starts with some specific character/s

Use below command if you want to output those records which starts with ‘c’. Below command will output values starting with character ‘c’.

UNIX GREP COMMAND EXAMPLES

Below command will output values starting with ‘ch

UNIX GREP COMMAND EXAMPLES

(You can also use the search option like these: ^”ch” or “^ch”. Output will be same)

5) Match pattern which ends with some specific character/s

Use below command to search pattern which ends with ‘e’

UNIX GREP COMMAND EXAMPLES

Use below command to output those patterns which ends with ‘il’

UNIX GREP COMMAND EXAMPLES

(Here also you can also use the search option like these: ”il$” or “il”$. Output will be same)

6) Match pattern which start with some character/s and ends with some character/s

Output those values which start with ‘p’ and end with ‘n’
Use ^p to tell that pattern should start with ‘p’.
Use n$ to tell that pattern should end with n
Use .* to tell that any thing can be present between ‘p’ and ‘n’.

UNIX GREP COMMAND EXAMPLES

(Try and explore using single/double quotes in the above command options)

7) Count of the number of matches

Sometimes you don’t want to output the searched pattern, you just want to know how many times searched pattern occurs in the file. Use -c option to check how many times ‘pen’(case sensitive) pattern appears in the file

UNIX GREP COMMAND EXAMPLES

We can cross verify by running our basic command. We are searching case sensitive ‘pen’ search. Hence ‘Pen’ will not be counted. Also we have not done exact pattern search using -w hence pencil is coming in output.

UNIX GREP COMMAND EXAMPLES

8) Print only the matched pattern

So far, our commands printed the whole text of the matched pattern. What if you just want to print the matched pattern? Confused!!!! Look at the example below:

The output is from the record having ‘cup’ value. But instead of printing whole value ‘cup’, it just printed ‘cu’ which is the pattern we searched for.

UNIX GREP COMMAND EXAMPLES

9) Print the line number of the matched pattern

If you want to know the line number where your search pattern appears then use below command.

We have use two examples here. First one gives the line number of the pattern ‘pen’. This is matched in ‘pen’ and ‘pencil’. Second one gives the line number of ‘bo’. Note that it outputs the complete record not just ‘bo

UNIX GREP COMMAND EXAMPLES

10) Byte position of the match

This command will give the byte position of the match.

UNIX GREP COMMAND EXAMPLES

The byte position starts with index 0. Hence the first match of ‘p’ is at 0. Then at the end of each line, there is one end-of-line character. Hence second ‘p’ is at 4(0-p, 1-e, 2-n, 3-eol). Last ‘p’ is from ‘cup’. Here the byte position of ‘c’ will be taken not ‘p

11) Match pattern which contain some characters in between (Relate this command with point no 6)

In below command we want to print those record which have ‘e’ and ‘i’ in them with any combination of characters in between them. Second command with -o option will print only the matched pattern.

UNIX GREP COMMAND EXAMPLES

12) Print records which does not match the searched pattern

Use -v option to output record which does not match the pattern. This command will output all records which does not have ‘table’in their pattern

UNIX GREP COMMAND EXAMPLES

Use below command to output records which does not contain ‘chair’, ‘table’, ‘cup

UNIX GREP COMMAND EXAMPLES

13) Count of unmatchedrecords (related to point 7)

Like before, you can count the number of unmatched records using -c. Sequence of -v/-c will not matter.

UNIX GREP COMMAND EXAMPLES

14) Print the matched patternin all the existing files

Below command will output the matched instances of pattern ‘book’ in all the files. I have total 3 files having pattern ‘book’ hence you can three different file names. You will see the complete record of the matched pattern after the file name

UNIX GREP COMMAND EXAMPLES

We can also use ‘-r’ option with the above command like this

UNIX GREP COMMAND EXAMPLES

15) Print the matched pattern in some specific files

Let’s assume that we have another file named test_grep2havingexactly same data. Then try to search for pattern ‘table’ in these two files using below command. It will give the details of matched pattern in all the files having name of test_grep* pattern

UNIX GREP COMMAND EXAMPLES

16) Print some records before and after the match record

Use below command to print 2 lines after the matched record including the matched records. In below output, ‘chair’ is matched at 4th record. -A 2 will print two records after the match

UNIX GREP COMMAND EXAMPLES

Use below command to print 3 lines before the match. In below command, ‘cup’ is matched at 6th record. -B 3 will print 3 records before this match

UNIX GREP COMMAND EXAMPLES

Use below command to print 2 lines before and after the match. -C 2 will print 2 records before and after the match

UNIX GREP COMMAND EXAMPLES

17) There is also a command which will highlight your matched pattern in some selected color

This is a bit tricky but can be tried for some fun. Use below commands:
export GREP_OPTIONS=’–color=auto’
GREP_COLOR=’/00;8’
grep pen test_grep

In the output, pattern ‘pen’ will be highlighted.

Sometimes you will get error ‘grep: warning: GREP_OPTIONS is deprecated; please use an alias or script’. To solve this error, remove below line from .exports file

#Always enable colored grep output
export GREP_OPTIONS=”–color=auto”

And add below to .alias

#Move export GREP_OPTIONS=”–color=auto” (which is deprecated) from .exports to .alias
#Always enable colored grep output`
alias grep=”grep –color=auto”
alias fgrep=”fgrep –color=auto”
alias egrep=”egrep –color=auto”

(UNIX Awk Command: https://itkaksha.com/unix-awk-command-examples-part-1/ )

Leave a Reply

Your email address will not be published. Required fields are marked *

Python 3.11.1 Features