Like this CertiGuide? Get it in PDF format!
Click Here!

Custom Search







Table Of Contents  CertiGuide to A+ (A+ 4 Real)
 9  Chapter 5: The Linux Operating System
      9  Basic Concepts and Procedures for Creating, Viewing and Editing Files and Directories
           9  Common Commands

Previous Topic/Section
diff - Find Differences Between Two Files
Previous Page
Pages in Current Topic/Section
1
Next Page
jobs - List Jobs that are Running from the Current Shell
Next Topic/Section

grep - Print Lines Matching A Pattern

grep is a very useful command which will search through files to find lines those contents match a given pattern. For example the command:

grep phil /etc/passwd

Will search the file /etc/passwd (remember that /etc/passwd it is where the user IDs and other important user information is kept) and return the line or lines that matches the word phil, as shown

phil:x:500:500:Phil:/home/phil:/bin/bash

This is a useful way to find the location of Phil’s home directory and the shell he uses.

The general form is:

grep [options] pattern [filename]

Where pattern is any text sequence or is a regular expression. This does a search through any file or if file name is the name of a directory followed by the wild card * (i.e. /dir/* ) it will search the file or the files within that directory for lines in those files that match the pattern. When a match to the pattern is found within a line, it will output that line. If this was a search on multiple files, it will also start the line with the name of the file where the match was followed by the text of the line that matched.

Regular expressions are a way to allow greater flexibility in the search pattern in a similar manner that Meta characters provide expanded capabilities to the shell when searching for filenames. Some of the regular expression characters are the same as Meta characters, for example the *, though it has a different functionality. Books have been written explaining the full power of regular expressions all that can be covered here is the basic definition of some of the characters.

Table 15 shows a listing of a few of the regular expression characters in Linux. Note that the function of the * is considerably different then it is used as a Meta character in that it matches zero or more occurrences of the preceding character. To learn more about regular expressions check out one of the many resources on the Web.


Table 15: Regular Expression Characters

Character

Description

^

Beginning of the line

$

End of line

.

And single character except end of line

*

Zero or more of the preceding character

{abc}

Match one of the characters in the brackets

{a-n]

Match the range of characters in the brackets

[^abc]

Match everything but the characters in the brackets


Previous Topic/Section
diff - Find Differences Between Two Files
Previous Page
Pages in Current Topic/Section
1
Next Page
jobs - List Jobs that are Running from the Current Shell
Next Topic/Section

If you find CertiGuide.com useful, please consider making a small Paypal donation to help the site, using one of the buttons below. You can also donate a custom amount using the far right button (not less than $1 please, or PayPal gets most/all of your money!) In lieu of a larger donation, you may wish to consider buying an inexpensive PDF equivalent of the CertiGuide to A+ (A+ 4 Real) from StudyExam4Less.com. Thanks for your support!
Donate $2
Donate $5
Donate $10
Donate $20
Donate $30
Donate: $



Home - Table Of Contents - Contact Us

CertiGuide to A+ (A+ 4 Real) (http://www.CertiGuide.com/apfr/) on CertiGuide.com
Version 1.0 - Version Date: March 29, 2005

Adapted with permission from a work created by Tcat Houser et al.
CertiGuide.com Version Copyright 2005 Charles M. Kozierok. All Rights Reserved.
Not responsible for any loss resulting from the use of this site.