Grep Command Tutorial For Unix
In this case, the command looks for lines in filename that have both regexp and regexp2. More specifically, grep will search for regexp2 in the results of the grep search for regexp. Using the previous example, if an administrator wanted to see every date that Smith edited any file except hi.txt, he could issue the fol- lowing command:
cat editinginfo | grep Smith | grep -v hi.txt
The following output would result:
June 21, 2008 Smith world.txt
It is important to note that “chaining” grep commands is inef- ficient most of the time. Often, a regular expression can be crafted to combine several conditions into a single search.
For instance, instead of the previous example, which combines three different commands, the same could be accomplished with:
grep Smith | grep -v hi.txt
Using the pipe character will run one command and give the results of that command to the next command in the sequence In this case, grep searches for lines with “Smith” in them and sends those results to the next grep command, which excludes lines that have “hi.txt”. When a search can be accomplished using fewer commands or with fewer decisions having to be made, the more efficiently it will behave. For small files, per- formance isn’t an issue, but when searching through gigabyte- sized logfiles, performance can be an important consideration.
There is a case to be made for piping commands when you wish to search through content that is continually streaming. For instance, if you want to monitor a logfile in real-time for speci- fied content, she could use the following command:
tail -f /var/log/messages | grep WARNING
This command would open up the last 10 lines of the /var/log/messages files (usually the main system logfile on a Linux system), but keep the file open and print all content placed into the file as long as it is running (the -f option to tail is often called “follow”). So the command just shown would look for any entry that has the string “WARNING” in it, display it to the console, and disregard all other messages.
As an important note, grep will search through a line and once it sees a newline, it will restart the entire search on the next line. This means that if you are searching for a sentence with grep, there is a very real possibility that a newline character in the middle of the sentence in the file will prevent you from finding that sentence directly. Even specifying the newline character in the search pattern will not alleviate this problem. Some text editors and productivity applications simply wrap words on lines without placing a newline character, so search- ing is not pointless in these cases, but it is an important limi- tation to keep in mind.
To get details about the regular expression implementation on your specific machine, check the regex and re_format man- pages. It is important to note, however, that not all the func- tions and abilities of regular expressions are built-in to grep. For instance, search and replace is not available. More importantly, there are some useful escape characters that seem to be missing by default.
For instance, \d is an escape sequence to match any numeric character (0 through 9) in some regular expressions. However, this does not seem to be available with grep under standard distribution and compile options (with the exception of Perl- style grep, to be covered later). This guide attempts to cover what is available by default in a standard installation and at- tempts to be the authoritative resource on the abilities and limits of grep.
The grep program is actually a package of four different pattern-matching programs that use different regular- expression models. Each pattern-matching system has its strengths and weaknesses, and each will be discussed in detail in the following sections. We’ll start with the original model, which we’ll call basic grep.
Basic Regular Expressions (grep or grep -G)
This section focuses on basic grep. Most of the flags for basic grep apply equally to the other versions, which we’ll discuss later.
Basic grep, or grep -G, is the default pattern matching type that is used when calling grep. grep interprets the given set of pat- terns as a basic regular expression when it executes the com- mand. This is the default grep program that is called, so the -G option is almost always redundant.
In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.