Regular Expression

Regular Expression in Linux

Regular expressions (Regexp) :-Regular Expressions is string with combination of character and meta-character . which is used for searching of string.

Types of Regular Expressions:- Below are the category of RE

1) Basic RE
2) Interval RE
3) Extended RE

Basic RE:- Regular Expressions which do not require any options to execute.

Below is the list of Regular Expressions


Meta Character
Matches
^
Beginning of the line
$
End of the line
*
0 or more occurrence of the previous character
.
To match any one character
[]
Character in set /Range
[^]
Character not in set/Range
\<
Beginning of word anchor
>/
End of word anchor
x\{m\}
Repetition of character x, m times
x\{m,\}
Repetition of character x, at least m times
x\{m,n\}
Repetition of character x between m and n times
x\{,n\}
Repetition of character x ,from 0 to  n times
?
Matches previous atom 0 or 1 time
+
Matches previous atom one or more times


Examples:- 1) Find all the files in a given directory
            ls -l | grep ^-

Note:- In ls -l output, First character – is for regular files and d for directories in a given folder.The ^ symbol is for matching line starting,
       ^- indicates what ever lines starts with -.

          2) Find all the directories in a folder
             ls -l | grep ^d
          3) Find all the block file in a directory
             ls -l | grep ^b
          4) Fild the all commented lines in a file
             grep '^#' filename
          5) Find all lines start with abc in a file
             grep '^abc' filename
          6) Find all the files which ends with sh
             ls -l | grep sh$
          7) Find all the files which ends with log
             ls -l | grep log$

Note:-$ indicates end of the line, the above command will list all the files whose names end with sh.

          8) Find all lines in a file which ends with dead
             grep 'dead$' filename
          9) Find all empty lines in a file
             grep '^$' filename
          10) Find all files with name awt, awet, aweet etc in the file name.
             ls -l | grep 'aw*t'
          11) Find all files which contains any single character between a and b in a file name.
             ls -l | grep 'a.b'

Note:- "." match any single character.

          12) Find all the files which name starts with c and end with h using regular expressions?
             ls -l | grep 'c.*h'

Note:- "." indicates any one characters and "*" indicates it can be repeated(*) 0 or more number of times .

          13) Find all the files which contains a number in the file name between a and x
             ls -l | grep 'a[0-9]x'

Range Operator:-
[a-z] –Match's any single char between a to z.
[A-Z] –Match's any single char between A to Z.
[0-9] –Match's any single char between 0 to 9.
[a-zA-Z0-9] – Match's any single character either a to z or A to Z or 0 to 9
[!@#$%^] — Match's any ! or @ or # or $ or % or ^ character.

          14) Find all the files names except a or b or c in it’s filenames
              ls | grep  '[^abc]'
          15) Find lines in file that contain  abc.
              grep '' filename
          16) Find files which contain [ in it’s content, as [ is a special charter we have to escape it
              grep "[" filename    
                    OR
              grep '[[]' filename

No comments:

Post a Comment