Learn basic regex for Linux in 2 minutes

re  :  strings in which
^a : a is the beginning char
a$ : a is the end char
a? : 0 or 1 a
a+ : 1 or more a
a* : 0 or more a
^a : doesn't contains a
. : matches any character


{3,5} : range of 3 to 5
[abcf] : matches any one of a,b,c or f
[[:space:]] : matches spaces
[[:space:]]{4,} : matches 4 or more spaces
[a-z] : a to z
[a-zA-Z] :  a to z and A to Z

d : any digit
s : space
w : any alphabet

w+(.ab)w+(53) : grouping (.ab) into group 1 and (53) into group 2, use them using \1,\2




Exercises:

1. ab(cd)ef to abcdef
 rename -f 's/\([a-zA-Z0-9]+\)//g' *

2. ab_cde_f to ab cde f
 rename -f 's/_/ /g' *

3. 0abcde to abcde
 rename -f 's/(0)+([0-9a-zA-Z-.]+)/$2/g' *

4. a.enbc to a bc
 rename -f  's/\.en/ /g' *

5. a-bcdef to a bcdef
 rename -f  's/-/ /g' *

6. AbcDeF to abcdef
 rename -f  'y/A-Z/a-z/' *

Comments

Popular posts from this blog

How to setup a DC hub in your local network

Some basic linux hacks