Use grep to print lines match a pattern

Share this post to your friends !

This entry is part 7 of 10 in the series Linux commands: Part 6 - Redirection

WriteBash - 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 - writebash.com
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.

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

If you look at the image above, there is a line contain string writebash.com 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.

linuxmint writebash.com # ls /bin | grep zip
bunzip2
bzip2
bzip2recover
gunzip
gzip
use-grep-to-print-lines-match-a-pattern-02 Use grep to print lines match a pattern
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.

Conclusion

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.

Continue reading the series«« Previous part: Use wc command to count number linesNext part: Use head command to print first part of files »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.