Regular expressions

From LinuxIntro
Revision as of 11:27, 14 September 2008 by WikiSysop (Talk)

Jump to: navigation, search

Regular expressions allow you to formulate patterns to search for. Here's an example: It is easy to search for the string "Sep" in a file, you do it with

grep "Sep" file

This gives you all lines containing the string "Sep". But what do you do if you only want lines starting with "Sep", for example, to read all lines in your syslog regarding september? Then you need regular expressions. It works like this:

grep -E "^Sep" /var/log/messages

gives you all entries for september in your syslog. And there is much more you can do with regular expressions.


Match string1 OR string2

grep -E "Sep|Aug" file

prints all lines that contain "Sep" or "Aug".