6.4 Standard and Extended Regular Expressions

   

Sometimes you may want to make logical OR operations in regular expressions. As an example, you may need to find all lines in your saved files in the $HOME/mbox file containing a sender's address and date of sending. All such lines start with the words " From: " and " Date: ". Using a standard regular expression it would be very difficult to extract this information. The egrep command uses an extended regular expression as opposed to the grep command that uses standard regular expressions. If you use parentheses and the logical OR operator ( ) in extended regular expressions with the egrep command, the above-mentioned information can be extracted as follows .

 $  egrep '^(FromDate):' $HOME/mbox  

Note that we don't use \ prior to parentheses in extended regular expressions.

You may think that this task can also be accomplished using a standard regular expression with the following command; it might seem correct at the first sight but it is not.

 $  grep '[FD][ra][ot][me]:' $HOME/mbox  

This command does not work because it will also expand to " Fate ," " Drom ," " Droe ," and so on.

Extended regular expressions are used with the egrep and awk commands. Sometimes it is more convenient to use standard expressions. At other times, extended regular expressions may be more useful. There is no hard and fast rule as to which type of expression you should use. I use both of these and sometimes combine commands using both types of expressions with pipes to get a desired result. With practice you will come to know the appropriate use.


   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

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