Server Administration
  Home arrow Server Administration arrow Page 4 - Regular Expressions in the Unix Shell
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
SERVER ADMINISTRATION

Regular Expressions in the Unix Shell
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2009-06-10

    Table of Contents:
  • Regular Expressions in the Unix Shell
  • Extended Regular Expressions
  • Explaining and Examples
  • The grep command

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Regular Expressions in the Unix Shell - The grep command


    (Page 4 of 4 )

    The grep command is what will take the regular expression we just learned on the previous page, and try to match it in an input file. The input comes through the standard input, so if we want to search in a file we will use the cat command and a pipe. If we want to perform the search on a specific text, we use the echo and the pipe. Additionally, you can get the source from any other place.

    The grep command will automatically set the $? to true if a valid match is found, or false otherwise. By default, grep will print back the lines in which it managed to make a match. If multiple matches are possible, the line will be printed only once. If the GREP_OPTIONS has the -color=auto specified, the command will colorize the matches.

    To set this as a default behavior of the command, you need to add the following line at the end of the .bashrc file in the home folder:

    export GREP_OPTION='-color=auto'

    The table below will enumerate the options possible for the grep command:

    Option

    What it does

    -c

    Count the number of matches and return the result

    -E

    Work with the extended regular expressions

    -e pattern

    We can define the pattern this way also; otherwise, it will take the first text argument as the pattern

    -P

    Work with Perl regular expressions

    -f file

    Use the file specified instead of the standard input

    -i

    Ignore differences between capital and lower case letters

    -L

    In the case of multiple files, return the one in which there is not a match

    -l

    In the case of multiple files return the one in which there is a match

    -m number

    After m number of matches, stop

    -n

    Before the match, print the number of matches

    -o

    Print only the matches

    -q

    Do not print anything. Just set the $? special variable if there was or not a match

    -r or -R

    Search recursively in the given directory

    -s

    Do not print an error message for those files that the command cannot read

    -v

    Print the lines in which there was no match

    -w

    A match is considered correct only if there is a single word, or more precisely the pattern is surrounded by the b character.

    -x

    Only matches forming a full line (^pattern$)

    The grep command uses a greedy matching algorithm, meaning that it will try to match the pattern to the longest sequence possible. For example, with the "a.*b" pattern, on the following text, the match will be:

    aaabbbbaaaaabbbbbba

    Finally, a couple of concrete examples:

    if echo "$variable" | grep -E '^Yeti' >/dev/null 2>&1

    then

    echo The variable starts with Yeti

    fi

    if cat alfa| grep -q -E '^alfa$'

    then

    echo alfa found in the file alfa in a single line

    fi

    if file "$variable" | grep -q -E "text"

    then

    echo The "$variable" is a text file

    fi

    There could be countless more examples. You can create a pattern to find all the dates or to find all the positive numbers, for instance. I will leave to you play around with this inside the terminal. Before you will do this, please rate my article and post your questions on the blog following this article. Next time I will present the sed command, which stands for stream editor. Please make sure you come back. Live With Passion!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    SERVER ADMINISTRATION ARTICLES

    - Processes in the UNIX Shell
    - Migrating from Windows to Wine
    - Wine: Not Another Emulator
    - Preventive Measures to Block SSH Attacks
    - Monitoring Temperatures with Cacti
    - Cacti: RRDTool-based Graphing Solution
    - Network Magic 5.0 Review
    - Netfilter and Iptables Overview
    - Installing and Configuring Squid
    - Clickfree PC Backup Systems Compared
    - Squid, the Caching Proxy
    - Regular Expressions in the Unix Shell
    - Source Code Version Control Solutions
    - OTRS: Open Source Ticket Request System
    - Clonezilla: Free Mass Disk-Cloning Utility





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek