Technology blog::Life hacks::Linux::Hardware::Gaming

Using Grep to Search for Text in Linux

Finding things is easy, if you know how to look...

By , in Linux

Using Grep to Search for Text in Linux
Grep is a Unix tool used for finding text within files. This tool is very simple to use, much misunderstood and very powerful. It is an essential command to master when using Unix and Linux.

Grep is the Linux equivalent of Windows Find in Files. Grep can use regular expressions to search files or output for text, it can also use plain text searches.

To search for files containing a particular string, it is as easy as typing in

grep "findme"

This will list out the files and the text surrounding the match. You can add the -r flag to search recursively.

grep -r "findme"

You can also search the output of other commands, for example, a list of the currently installed packages. To see a list of the currently installed packages is a simple command, however, the output is very large and can be difficult to locate all the installed Apache packages.

sudo apt --installed list

To make things easier, we can pipe this output to the grep command which will then search and show only the packages with Apache in the name.

sudo apt --installed list | grep apache
apache2/now 2.4.7-1ubuntu4.13 amd64 [installed,upgradable to: 2.4.7-1ubuntu4.15]
apache2-bin/now 2.4.7-1ubuntu4.13 amd64 [installed,upgradable to: 2.4.7-1ubuntu4.15]
apache2-data/now 2.4.7-1ubuntu4.13 all [installed,upgradable to: 2.4.7-1ubuntu4.15]
apache2-mpm-prefork/now 2.4.7-1ubuntu4.13 amd64 [installed,upgradable to: 2.4.7-1ubuntu4.15]
apache2-utils/now 2.4.7-1ubuntu4.13 amd64 [installed,upgradable to: 2.4.7-1ubuntu4.15]
libapache2-mod-php5/trusty-updates,trusty-security,now 5.5.9+dfsg-1ubuntu4.21 amd64 [installed]
libapache2-mod-svn/trusty-updates,trusty-security,now 1.8.8-1ubuntu3.2 amd64 [installed]
libapache2-svn/trusty-updates,trusty-security,now 1.8.8-1ubuntu3.2 all [installed]

Using Regular Expressions with Grep

Using regular expressions with grep allows us to search for text beginning or ending with a string. These commands can work on files or the piped output of a command. In these examples, I'm just working on a file for ease of demonstration.

You have to use the -E flag for enhanced search, which allows the use of regex. This command will show files starting with fig.

grep -E ^fig /usr/share/dict/words

This will show files ending with ion

grep -E ion$ /usr/share/dict/words

This will show the lines where the word toon is a word, that is preceded and followed by a word boundary (spaces, punctuations, carriage returns etc.)

grep -E '*toon*' /usr/share/dict/words

This shows matches which start with po, contain any two characters, and ends in ute.

grep -E '^po..ute$' /usr/share/dict/words

And this shows all matches which contain any 5 of the specified letters in the brackets

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

You can read more about Regular Expressions in this other tutorial.

Last updated: 2017-06-17

Further Reading

There are no comments for this post. Be the first!

Leave a Reply

Your email address will not be published.