Examples of using regular expressions

To search using enclosed characters:

   grep -n '[dD]on't' tasks

This uses a regular expression to find and display each line in the file tasks that contains the pattern don't or Don't. The line number for each line is also displayed.


To turn off the meaning of a special character:

   grep '^\$' money

This lists all the lines in the file money that begin with a $ (dollar) sign. This character is preceded by a \(backslash) to remove its meaning as a special character.


To search from the beginning of a line:

   ls -l | grep '^d........x'

This lists all the directories in the current directory for which other users have execute permission.


To search for lines ending in a particular pattern:

   ls -l | grep '[^.xdh]$'

This lists all the files and directories in the current directory which do not end in .xdh.


[Home] [Search] [Index] This site maintained by unixhelp@ed.ac.uk