Open In App

Shell Script for grep with context display and highlighted pattern matches

Improve
Improve
Like Article
Like
Save
Share
Report

grep ( global search for regular expression and print out ) is a filtering command used for searching a particular pattern of characters in a file. It offers different options for manipulating the searched result, a brief description of these options can be found in this article.

General Syntax :

$ grep [option(s)] pattern [file(s)] 

One of the options offered with the grep command is –color which highlights the searched pattern in color, making it more convenient for the user to recognize. 

Example: Consider file.txt with the following content and pattern to be searched is “the”

Hello geek!

I am a demo file.

–color option can be used for highlighting the searched text.

Like this article, if you understood the concept.

Command:

$ grep --color 'the' file.txt

Output:

The output of the above Command

The output of the above Command

You can observe that it outputs only the lines which contain the searched pattern. But if we want to display the whole content with the highlighted pattern, we can use the OR operator in the pattern and pass a metacharacter as the second pattern. The two metacharacters which can be used here are ^ and $. ^ stands for the beginning of a text line and $ stands for the end of a line. Obviously, all the lines will have a beginning and end so all the lines will get printed without anything extra highlighted. 

Command:

$ grep --color 'the\|^' file.txt 
                  OR
$ grep --color 'the\|$' file.txt 

Note: We are required to use the escape ‘\’ character for escaping the OR operator so that grep won’t consider the OR symbol ‘|’ as part of the pattern. Alternatively, we can use the -E option to directly use the OR operator in the pattern and the command will look like this:

$ grep -E --color 'the|^' file.txt
               OR
$ grep -E --color 'the|$' file.txt 

Output:

 

Syntax Explanation :

General Syntax 1: $ grep –color ‘pattern\|^’ filename 

  • grep ->  Filtering Command
  • –color ->  Highlights the searched Pattern 
  • pattern ->  write the pattern to be searched, for multiple patterns use the escape and OR operator 
  • A metacharacter ->  ‘^’ meaning start of a text line  or ‘$’  meaning end of line  
  • filename ->  name of the file or files 

you can write multiple file names like [ $ grep –color ‘pattern\|$’ file.txt file2.txt ]

General Syntax 2: $ grep -E –color ‘pattern|^’ filename 

Using the -E options you can directly use the OR operator in the pattern  

Now after understanding how we can use the grep command, Let’s look at the shell script implementing the same.

#!/bin/sh

echo “Enter the file name(s) : “;  

read filename     # reads the input and stores file name(s) in variable named filename

echo “Enter the Pattern : “;

read Pattern      # reads the input and stores it in a variable named Pattern

grep –color -E “$Pattern|^” $filename  # grep filters the pattern $Pattern in file with name $filename 

# grep –color “$Pattern|\’$'” $filename

# Both LINE 6 and LINE 7 will produce the same Output

Output:

 

Code Explanation:

  • We have asked the user to input the file name and stored it in a variable named $filename.
  • Then we have taken the input for a pattern to be searched and stored it in another variable named $Pattern.
  • Finally, we have passed the $Pattern and $filename in the grep command as we have learned above.

Conclusion

We have learned how to use the grep command for getting the highlighted text. Also, it can be concluded that grep directly does not offer any option to print the whole content with the highlighted pattern but we can use this trick of passing metacharacters as a second parameter along with the –color option in the grep command to get the required output.


Last Updated : 05 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads