Use grep to print lines match a pattern

by Daniel Pham
Published: Updated:
This entry is part 7 of 10 in the series Linux commands: Part 6 - Redirection

This article will guide you to the grep command. How to use grep to print lines match a pattern. The grep command is an extremely powerful command in Linux, especially when you use it in the pipeline.

Grep command syntax

We have two ways to use this command.

First, we use this command to find for text patterns in the file.

$ grep pattern [file...]

Second, we use this command to find for patterns in the standard output of pipeline.

$ command-1 | ... | grep pattern | ... command-n

Example use grep to print lines that match a pattern

Example find a pattern in a file

First, we will do the example with the first use. We will find a pattern in a file, the grep command will print the lines containing that pattern.

For example, we have the file with the following content, the file name grep-demo.txt.

line 1 -
line 2 - demo content 2
line 3 - my name is Danie Pham
line 4 - writebash.demo
line 5 - write.bash

Next, we will use grep to print the lines containing the pattern writebash.

$ grep writebash grep-demo.txt 
line 1 -
line 4 - writebash.demo
use grep to print lines match a pattern in the file
Use grep to print lines match a pattern in the file.

If you look at the image above, there is a line contain string and a line contain string writebash.demo. Although two lines are different, grep command prints it all, because it contains a pattern writebash.

If you want to find the line containing only the exact writebash string, you add the -w option to the command.

$ grep -w writebash grep-demo.txt

For help with grep commmand, you can type this command and read about all options:

$ grep --help

Example find a pattern in the pipeline data

Now, we will do an example of using grep in the pipeline.

I use a familiar example, listing all the files in the /bin directory. And then, I will use the grep command to find all files with word zip in its name.

$ ls /bin | grep zip
use grep to find all files contain word "zip" in /bin folder
Use grep to find all files contain word “zip” in /bin folder.

The data in the pipeline after processing through grep, can continue processing with another command.

Grep and regular expressions

Above, I only do simple examples with simple words. In fact, you will have to find much more complex patterns, for example: email address, email login information, etc.

To do that, you need to use advanced patterns and it calls regular expressions. In this article, I will not mention it, but can you find out more about regular expressions?

I will talk about regular expressions in the next article.


The grep command is a really powerful command, probably all Linux users have to admit it. Find out more about it, it depends on you and how much you practice it.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series Navigation«« Previous part: Use wc command to count number linesNext part: Use head command to print first part of files »»

You may also like

Notify of
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.



Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 – All rights reserved.

Please write sources “” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Would love your thoughts, please comment.x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.