Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))

grep and egrep Regular Expressions
Grep and egrep are Unix and Linux utilities which filter an incoming file, outputting all lines which match a regular expression and not outputting lines that fail to match. The original grep uses a somewhat restricted regular expression set, and egrep (in full, the extended global regular expression processor) has somewhat further facilities. Fgrep (the f stands for fake) doesn't use regular expressions at all - it checks for literal strings.

      grep cat demo.txt
outputs all lines from the file demo.txt that include "cat"

Special characters in regular expressions such as [ ] ? and * also have a special significance to the Unix or Linux shell, and it's usual to write your regular expression in quotes to protect it from the shell.

Operator TypeExamplesDescription
Literal Characters
Match a character exactly
a A y 6 % @Letters, digits and many special
characters match exactly
\$ \^ \+ \\ \?Precede other special characters
with a \ to cancel their regex special meaning
\n \t \rLiteral new line, tab, return
Anchors and assertions ^Starts with
$Ends with
Character groups
any 1 character from the group
[aAeEiou]any character listed from [ to ]
[^aAeEiou]any character except aAeEio or u
[a-fA-F0-9]any hex character (0 to 9 or a to f)
.any character at all
apply to previous element
+1 or more ("some")
egrep only
*0 or more ("perhaps some")
?0 or 1 ("perhaps a")
Alternation |either, or
egrep only
Grouping ( )group for count and save to variable
egrep only

Additionally, some versions of egrep (especially linux) support numerous extra sequences such as POSIX character groups (e.g. [[:alnum:]] for an alphanumeric character) and extended counts (e.g. {4,8} for bewteen 4 and 8 of the previous element). Take care when using these extras if you want your scripts to be portable.

grep, egrep and fgrep all take a number of options to allow you further control over your matching and output. The options don't differ between egrep, fgrep and grep but there are more options available under linux than there are under Unix. The following table includes the more common options under both operating systems.

-iIgnore case in matching
-vOutput only lines which do NOT match
-cOutput only a count of the numer of lines that match
-lOutput only the names of input files that include at least one match
-nOutput the line number within the file in from of each matched line
-hSupress file name output even if there are multiple input files

The -r option (not available on all versions of grep) allows the recursive searching of all files in subdirectories.

Back to Regular Expression Home Page
Jump to Elements of a regular expression
Order a Regular Expression Mousemat for £4.95 inclusive

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2023: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho