OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

grep – Print Lines Matching A Pattern

grep is a powerful program used to find text patterns within files. It's used like this:


grep pattern [file...]

grep pattern [file...]


When grep encounters a “pattern” in the file, it prints out the lines containing it. The patterns that grep can match can be very complex, but for now we will concentrate on simple text matches. We'll cover the advanced patterns, called regular expressions in a later chapter.

Let's say we wanted to find all the files in our list of programs that had the word “zip” embedded in the name. Such a search might give us an idea of some of the programs on


our system that had something to do with file compression. We would do this:



[me@linuxbox ~]$ ls /bin /usr/bin | sort | uniq | grep zip

bunzip2 bzip2 gunzip gzip unzip zip zipcloak zipgrep zipinfo zipnote zipsplit

[me@linuxbox ~]$ ls /bin /usr/bin | sort | uniq | grep zip

bunzip2 bzip2 gunzip gzip unzip zip zipcloak zipgrep zipinfo zipnote zipsplit


There are a couple of handy options for grep: “-i” which causes grep to ignore case when performing the search (normally searches are case sensitive) and “-v” which tells grep to only print lines that do not match the pattern.


Top OS Cloud Computing at OnWorks: