/ factorpad.com / tech / linux-essentials / grep-command.html
An ad-free and cookie-free website.
Beginner
Learn to search for text patterns at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux grep Command Summary with Examples (3:30)
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.
-options | description |
---|---|
--color |
Highlight selected text in a different color |
-c |
Count the number of occurrences |
-n |
Print line numbers |
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.
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
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.
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.
Very good.
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.
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
Learn faster by combining this page with the video. Subscribe and follow @factorpad on Twitter for updates.
join
command,
hit Back.sed
, click Next./ factorpad.com / tech / linux-essentials / grep-command.html
A newly-updated free resource. Connect and refer a friend today.