FactorPad
Faster Learning Tutorials

Linux grep command summary with examples

grep allows you to search for text patterns in a file.
  1. Purpose - Learn what grep is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with grep.
  4. A tip - Finish off with one more insight.
Paul Alan Davis, CFA, October 21, 2016
Updated: August 1, 2018
In this tutorial, 39 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Next

~/ home  / tech  / linux essentials  / grep command


The Linux grep command

Beginner

Learn to search for text patterns at the Linux command line.

Video Tutorial

Linux grep command summary with examples (3:30)

Videos can also be accessed from the YouTube Playlist.

Video Script

The Command and Why You Need It

Our thirty-ninth word, or command to memorize is grep from our category Text.

grep allows you to search for text patterns in a file.

Recall from recent videos, we've been sorting, cutting, pasting and joining files together. Now we'll advance by searching whole files of text.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The join command has over 40 options, and the arguments represents the file you'd like to read.

Like most commands, help is available with double-dash --help. And because grep is so advanced and important, I'll keep it simple here and later refer you to a helpful reading.

So why is grep an important command? Well, just like the search feature in a word processor, this Linux command is very useful for finding text patterns. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Remember back a few videos ago, we saw a misspelled word in one of our text files? I typed "thrid", instead of "third", so let's use grep to look for it. I don't recall exactly where, but it was one of the videos in the 20s, I think. So let's use grep with the --color option and in quotes type "thrid" video2*.txt

$ grep --color "thrid" video2*.txt video20.txt:This is the thrid line $ clear

There it is, in video20.txt highlighted in a different color.

Then we'll add the -c option to count the number of places it has been misspelled, and broaden the search to include all files.

$ grep --color -c "thrid" video*.txt video05.txt:1 video11.txt:0 video16.txt:0 video19.txt:0 video20.txt:1 video21.txt:0 video23.txt:0 video24.txt:0 video26.txt:0 video27.txt:0 video31.txt:2 video32.txt:2 video33.txt:0 video34.txt:0 video35.txt:0 video36.txt:0 video37.txt:0 video38a.txt:0 video38b.txt:0 video38c.txt:0 $ clear

And wow, there are more than I thought.

And last, let's throw in the -n option to include a line number, so we can get after these and tidy up, because we do like accuracy.

$ grep --color -n "thrid" video*.txt video05.txt:3:This is the thrid line video20.txt:5:This is the thrid line video31.txt:3:This is the thrid line video31.txt:10:This is the thrid line video32.txt:13:iThis is the thrid line video32.txt:14:jThis is the thrid line

Very good.

A Final Tip

Okay now you know how to use grep. And you know the syntax for commands, options and arguments.

One last tip about the grep command. So I only touched on the vast power of grep. In the Comments section find a link to a helpful page on Wikipedia on what are called Regular Expressions.

Okay, thanks for visiting today. I hope this was a helpful introduction to the grep command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

Learn faster by combining this page with the video. Subscribe here.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the join command, hit Back.
  • To wrap up a 10-video set on text processing with automated editing using sed, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / grep command



 
 
Keywords:
linux grep command
linux grep
grep unix
grep search patterns
linux search text
linux grep examples
grep regular expressions
man grep
find grep
grep options
grep syntax
linux find text
grep summary
linux grep tutorial
linux text search
grep usage
linux find text patterns