Ticker

6/recent/ticker-posts

Regex in Linux

 


Regex Operators

^  $  .  *  +  {}  ?  |  []  ()  [^]
^ "The line begins with"
## Look for line that starts with same and PASS
$cat names.txt | grep "^same"
$grep '^PASS' /etc/login.defs

$ "The line ends with"
## Look for line that ends with number 7 and mail
$grep '7$' /etc/login.defs
$grep 'mail$' /etc/login.defs

. "Match any ONE character"
## Look for line that match c.t like cat, cut, execute, etc
## c..t  means caat or cuat or colt, etc
$grep -r 'c.t' /etc/

## Look for line that match work c.t like cut, cat, execute won't match though because of -w
$grep -wr 'c.t' /etc/
  • how to match period char like suppose if we are looking for period operator itself, we need to escape it.
## \: Escapong for spcl character
$grep '\.' /etc/login.defs


* : Match the Previous element 0 or more
## * command allow to match pervious letter
let* = lettt
$grep -r 'let*' /etc/
  • sequeneces that begins with forward slash
  • have zero or more characters in between and
  • ends with forward slash
$grep -r '/.*/' /etc/
/etc/abc.conf
/usr/share/local


+ : Match the previous element one or more times
$grep -r '0*' /etc/
It will also matches the line that contains no zero because it above makes optional

$grep -r '0\+' /etc/
  • It is little bit hard to keep track of where to use backslash where not so we can use egrep instead


Extended regular expression

use egrep instead of a regular grep
$grep -Er '0+' /etc/

OR

$egrep r '0+' /etc/




{} : Previous element can exist "this many" times
## Match the line that contains atleast 3 zeros
$egrep -r '0{3,}' /etc/

## Match the line 1 followed by atleast 000, alone 1 can be matched as well
## It can match 1 or 10 or 100 or 1000
$egrep -r '10{,3}' /etc/

## Match the line that contains exactly 3 zeros
$egrep -r '0{3}' /etc/





? : Make The Previous Element Optional
## If you are looking for word disable or disabled, basically d is optional in end
$egrep -r 'disabled?' /etc/



{} : Previous element can exist "this many" times
## if you are looking for zero between 3 to 5
$egrep -r '0{3,5}' /etc/


| :Match One Thing or the other
## if you are looking for line that contains word enabled or disabled
$egrep -r 'enabled|disabled' /etc/

## d optional
$egrep -ir 'enabled?|disabled?' /etc/


[] : Ranges or Sets
[a-z]  -> any small letter from a to z
[0-9] -> any numeric from 0 to 9
[abz954] -> any character that is a or b or z or 9 or 5 or 4 
## following expression matches cut or cat 
$egrep -r 'c[au]t' /etc/

## grep the special file such as /dev/sda OR /dev/sd0
## following expression uses * it matches very wide rage of expression, we can use ranges
$egrep -r '/dev/.*' /etc/ -> Not good enough

We can write like following however it doesn't also match digit at the end
$egrep -r '/dev/[a-z]*' /etc/


$egrep -r '/dev/[a-z]*[0-9]' /etc/ -> catch digit at the end but not sda

$egrep -r '/dev/[a-z]*[0-9]?' /etc/ -> much better



() : subexpressions
$egrep -r '/dev/([a-z]*[0-9]?)*' /etc/ -> tty0t0
  • /dev/tt0So - don't match
$egrep -r '/dev/(([a-z]|[A-Z])*[0-9]?)*' /etc/ -> tty0t0
  • /dev/term/a don't match



[^] : negated ranges or sets
http[^s] = http https

not exist in the position
http is not followed by s
$egrep -r 'http[^s]' /etc/
$egrep -r '/[^a-z]' /etc/


Notes:

  • sed also supports regex


Ref:

Post a Comment

0 Comments