Friday, May 2, 2014

Unix Shell Regular Expression Overview

1. Character Classes:

Unix defines a set of character classes to represent a set of characters, examples of common character classes in unix:

[:alpha:] alphabetic characters
[:digit:] numerica characters
[:alnum:] alphanumerica characters, including [:alpha:] and [:digit:]
[:lower:] lower case characters
[:upper:] upper case characters

test:
 #! /bin/bash  
 echo $1  
 echo $2  
 echo ${10}  

terminal:
5 commands are trying to do:
1) Get all lines containing characters
2) Get all lines containing characters and numbers
3) Get all lines containing numbers
4) Get all lines containing the lower-case characters
5) Get all lines containing the upper-case characters(get nothing)
 aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ grep [[:alpha:]] ./test  
 #! /bin/bash  
 echo $1  
 echo $2  
 echo ${10}  
 aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ grep [[:alnum:]] ./test  
 #! /bin/bash  
 echo $1  
 echo $2  
 echo ${10}  
 aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ grep [[:digit:]] ./test  
 echo $1  
 echo $2  
 echo ${10}  
 aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ grep [[:lower:]] ./test  
 #! /bin/bash  
 echo $1  
 echo $2  
 echo ${10}  
 aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ grep [[:upper:]] ./test  
 aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$  

2. Collating Symbols:
 Use [[.ch.]] to catch all lines containing "ch" in sequence. But in modern ubuntu operating system, collating symbols are not supported. If we want to catch "ch", just "grep ch"

3. Equivalent class:
[=e=] means all characters equal to 'e'. In other language locale, we may expect a lot of special characters equal to e.
 aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ grep [[=e=]] ./test  
 echo $1  
 echo $2  
 echo ${10}  

Note: character classes, collating symbols, and equivalent class only works in bracket!
 grep: character class syntax is [[:space:]], not [:space:]  
 aubinxia@aubinxia-VirtualBox:~/Desktop/xxdev$ grep [[:alpha:]] ./test  
 #! /bin/bash  
 echo $1*  
 echo $2*\  
 echo ${10}  

No comments:

Post a Comment