With that caution, let's look at the most basic of regular
expressions, the simple substring. To do this, we're going to use
grep
, a command that scans the contents of a file for a particular
regular expression. grep
prints every line that matches the
regular expression, and ignores every line that doesn't:
$ grep bash /etc/passwd
operator:x:11:0:operator:/root:/bin/bash
root:x:0:0::/root:/bin/bash
ftp:x:40:1::/home/ftp:/bin/bash
Above, the first parameter to grep
is a regex; the second is a
filename. Grep
read each line in /etc/passwd
and
applied the simple substring regex bash
to it, looking for a
match. If a match was found, grep
printed out the entire line;
otherwise, the line was ignored.