10 Sensible Examples of the Linux Grep Command

The grep command provides access to the grep utility, a powerful file processing tool for finding patterns in text files. It has many practical use cases and is certainly one of the most widely used Linux commands. This guide shows some simple but useful Linux grep commands that are used in practice.

Example file for demonstration

We created a reference file to help readers understand grep better. You can make a copy of this file by running the following shell command in your terminal.

Cat <> Test file
This is a simple text file that contains
several strings as well as some phone numbers
(555) 555-1234 (567) 666-2345
and email plus web addresses

1. Find text in files

To find text patterns in a file, just run grep followed by the pattern name. Also include the name of the file that contains the text.

grep "email" test file

This command displays the line in ours Test file that contains the word E-mail. You can also grep the same text in multiple files.

grep "example" / usr / share / dict / american-english / usr / share / dict / british-english

The above command shows all instances of the word example by doing American English and British English Dictionary files.

2. Find exact match words

The Linux grep command shown in the previous example also lists lines with partial matches. Use the command given below when you just need the exact occurrence of a word.

grep -w "string" test file

The -w or –word-regexp The grep option restricts the output to only exact matches. Grep consists of a few additional flags that can also be used with the standard command.

Related: How did Grep get its name? The story behind Grep's creation

3. Ignore case distinctions

By default, grep looks for patterns that are case-sensitive. However, you may want to turn this off if you don't know beforehand which case the pattern is in.

grep -i "this" test file

Use the -I or – Ignore the case Option to turn off upper / lower case.

4. Count the number of patterns

The -c Flag stands for number. It shows the number of matches found for a given pattern. Administrators can use this to get certain information about the system.

You can grep the ps command to count the processes owned by the current user.

ps -ef | grep -USER

The following command displays the number of MP3 Files exist in a directory.

ls ~ / music | grep -c .mp3

5. Display line numbers with matches

You may want to find the line numbers that contain a particular match. Use the -n or – line number Option of grep to achieve this.

cat / etc / passwd | grep -n rubaiat

This option is particularly useful for debugging source code and troubleshooting log files. To display all numbers for lines in the ~ / .vimrc used to configure the Vim text editor:

grep -n "set" ~ / .vimrc

6. Search file names using extensions

To get a list of all MP3 Files in the ~ / Music Directory:

ls ~ / music / | grep ".mp3"

You can replace .mp3 with other extensions to find specific files. The following command lists them all php Files exist in the current working directory.

ls | grep ".php"

7. Look for patterns in compressed files

The Linux grep command can also find patterns in compressed files. You have to use that zgrep Order for it, however. First, create a compressed archive of our Test file by entering:

gzip test file

Now you can search for text or other patterns in the resulting archive.

zgrep email test-file.gz

8. Search for email addresses

Administrators can also list email addresses from text files using the Linux grep command. The following example searches for a regular expression pattern.

grep & # 39; ^ (a-zA-Z0-9) + @ (a-zA-Z0-9) + . (a-z) {2, } & # 39; Test file

You can find regular expressions for similar tasks or create them yourself if you know how they work.

9. Find phone numbers with grep

You can use grep regular expressions to filter out phone numbers from a text file. Note that you will need to adjust the pattern to match the type of phone numbers you need.

grep & # 39; ((((0-9) {3 }) | (0-9) {3 } ) (-) ? (0-9) {3 } ( -) ? (0-9) {4 } & # 39; test file

The above command filters out ten-digit American phone numbers.

10. Find URLs from source files

We can use the power of grep to list URLs in text files. The command given below will print all of them in Test file.

grep -E "^ (http | https | ftp): ( /) {2} ((a-zA-Z0-9 – .) + . (a-zA-Z) {2,4}) "Test file

We're using that again -E Extended regular expression option. You can use that too egrep Command to avoid adding.

egrep "^ (http | https | ftp): ( /) {2} ((a-zA-Z0-9 – .) + . (a-zA-Z) {2,4})" Test -File

Mastery of the Linux grep command

We presented some useful examples of the Linux grep command that can be used to solve real world problems. While these examples illustrate the power of grep for word processing, you must master regular expressions if you want to be extremely productive with grep.

Sometimes Linux users encounter certain situations when they cannot remember the various options of a command. Hopefully the Linux operating system gives you ways to get command line help for almost any system utility.

7 Ways to Get Command Line Help on Linux

All the essential commands for learning Linux commands from the command line

Continue reading

About the author

Rubaiat Hossain
(15 articles published)

Rubaiat is a CS graduate with a strong passion for open source. As well as being a Unix veteran, he's also been into network security, cryptography, and functional programming. He is an avid collector of used books and has an endless admiration for classic rock.

By Rubaiat Hossain

Subscribe to our newsletter

Sign up for our newsletter to receive tech tips, reviews, free e-books, and exclusive offers!

One more step …!

Please confirm your email address in the email we just sent you.

Leave a Reply

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