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
This is a useful way to find the location of Phils 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.
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.