04 Regular Expression - kumar159man/MyShellLearning GitHub Wiki
Let's create a file
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat regex.txt cat bat rat mat hat sat
Let's grab cat
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -E "[c]at" regex.txt cat
Let's grab cat, hat and sat
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -E "[chs]at" regex.txt cat hat sat
let's grab everything which starts with a-z and ends with at
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -E "[a-z]at" regex.txt cat bat rat mat hat sat
let's grab everything which starts with a-z and ends with at. Except bat and mat
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -E "[^bm]at" regex.txt cat rat hat sat
let's grab everything whose first letter is in range a-m and ends with at
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -E "[a-m]at" regex.txt cat bat mat hat
Let's grab words in capital too
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat regex.txt cat bat rat mat hat sat PAT Oat
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -E "[a-zA-Z]at" regex.txt cat bat rat mat hat sat Oat
Let's create a file
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat regex.txt bat rat 0983207447284 3984734327 map 43445 64 abcd
Let's grab only numbers from the file
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -E "[0-9]+" regex.txt 0983207447284 3984734327 43445 64
Let's grab valid mobile numbers from the file
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat regex.txt 1234567890 9753747484 9473930484749 3847439204 4747384748 4847474747720 44729567829034 9999444444Valid mobile number consist of 10 digits
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -wE "[0-9]{10}" regex.txt 1234567890 9753747484 3847439204 4747384748 9999444444
Grab words which are more than 7 characters
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat regex.txt mirinda limca tom jerry pepsi aquafina watermellon pineapple mackbookpro chelsea
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -wE "[a-z]{8,}" regex.txt aquafina watermellon pineapple mackbookpro
Grab words which have no. of charecters between 6-9
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -wE "[a-z]{6,9}" regex.txt mirinda aquafina pineapple chelsea
\w ->\w matches any word character (equal to [a-zA-Z0-9_])
- \s -> \s matches any whitespace character (equal to [\r\n\t\f\v ])
- \t -> \t matches a tab character
Grab name and valid mobile number
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat regex.txt manav 8765123467 raj 9812345678 rahul 7812345679 saurav 7345690132 ralph 8712345670 rocky 8123478562 shilpa 87676765744 shweta 09707986865987
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -wE "\w+\s[0-9]{10}" regex.txt manav 8765123467 raj 9812345678 rahul 7812345679 saurav 7345690132 ralph 8712345670 rocky 8123478562
Character Group Meaning
[:alnum:] Alphanumeric
[:cntrl:] Control Character
[:lower:] Lower case character
[:space:] Whitespace
[:alpha:] Alphabetic
[:digit:] Digit
[:print:] Printable character
[:upper:] Upper Case Character
[:blank:] whitespace, tabs, etc.
[:graph:] Printable and visible characters
[:punct:] Punctuation
[:xdigit:] Extended Digit
. is used to match a character with anything
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat regex.txt 4555556 4555557 4555889 45559999 45558769 4555508997 455555098098879878 4555557657890-987657890876879 4555vdbjsnjsfdgdkjf 4555fdsjghsdflksjgr@$32
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -oE "4555." regex.txt 45555 45555 45558 45559 45558 45555 45555 45555 4555v 4555f
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat regex.txt 341 34a 355 36asd 3adsdf 3@#!$
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -oE "34?" regex.txtin above34 34 3 3 3 3
grep -oE "34?" regex.txt
it will try to match 4 with one or 0 occurrence
- Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -oE "34*" regex.txt 3444444444444 344 3444 34 344 3444
^ use to match the beginning of a string
yubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat regex.txt apple anaconda icecream matchbox acer acronym aristocract tom jerry kinley bailley myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -E "^a" regex.txt apple anaconda acer acronym aristocract myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$
$ use to match the end of a string Let's fetch the words which ends wit t
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat regex.txt cat dog mat mouse dock rat bamboo mango hat icecream bat myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ grep -E "t$" regex.txt cat mat rat hat bat