Home Searching text with grep
Post
Cancel

Searching text with grep

Introduction


Linux operating system provides robust capabilities. Often it is deployed without a graphical interface (GUI). So trying to search for a specific text using the command line can be daunting, especially if you’re searching through hundreds of files. Case in point, looking for an event from the log files.

In this tutorial, we’ll cover the grep command to help simplify the process.

Shell Terminal Example


Here’s a quick demo how the commands work running on a replit instance.

Ignore case sensitivity

In demo 1 it runs the grep -i to ignore case sensitivity.

1
grep -i h word-list

Skip Characters

In demo 2 it runs grep -v to skip a character and -i again to ignore case sensitivity.

1
grep -vi h  word-list

Search starting character

In demo 3 it runs grep \< to search the begining character.

1
grep \<t word-list

Search ending character

In demo 4 it runs grep \> to search for the ending character

1
grep t\> word-list

Search special character

In demo 5 it runs grep '[[:punct:]]' to search special characters

1
grep '[[:punct:]]' word-list

Search for a fix digit interval

In demo 6 it runs grep -P '(?<!\d)\d{4}(?!\d)' using -P to leverage a perl expression.

1
grep -P '(?<!\d)\d{4}(?!\d)' word-list

Conculsion

The grep command is a powerful tool, there are more features not mentioned here. Run grep --help for the full guide.

This post is licensed under CC BY 4.0 by the author.