The grep Command


The grep Command

One of the most commonly used (and known) Unix commands is grep. Learning how to use grep will help you find files that contain a word or pattern. When the grep command is used properly, it can help you search through a file for something specific. For example, what if you wanted to search a file for a unique term?

When we talked about finding data on your Unix system, we narrowed the search down to different variables, such as the size of the file or the creation date. You can also narrow the search with grep.

You will soon learn that grep is one of the most powerful built-in programs in Unix. The program grep is also subdivided into more commands: grep, egrep, and fgrep. These three commands differ in what regular expressions they can handle. A regular expression is a pattern that can match various text strings. Regular expressions define a pattern of text that can be used to search files when a specific word or phrase to be searched for might not be known. To use grep,

1.

Choose the word or phrase you want to find.

2.

Find the filename you want to search.

3.

Type grep <pattern to find> <file or files to search> at the shell prompt.

For example:

 >grep "test" *.html index.html:you passed the test 

In this example, we see a quick grep of the word test in any file that resembles an HTML-based file. The grep command is a tool that will become even more valuable when you learn more about regular expressions.



    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net