Table of Content

Weight: 2

Description: Candidates should be able to manipulate files and text data using regular expressions. This objective includes creating simple regular expressions containing several notational elements. It also includes using regular expression tools to perform searches through a filesystem or file content.

Key Knowledge Areas:

  • Create simple regular expressions containing several notational elements
  • Use regular expression tools to perform searches through a filesystem or file content

Terms and Utilities:

grep
egrep
fgrep
sed
regex(7)

grep

grep -i 
grep -f file
grep -r # search specific folder, can use rgrep 

grep -r eth0 /etc/*
or 
rgrep eth0 /etc/*

sed

sed -f sed-script  file
sed script file

sed 's/2012/2013/g'  file1 > files

sed s/$/"\r"/   unixfie > dosfile

Quiz questions

1. Which commands support regular expressions?
2. Which command can one use to search for the word “ftp” at the beginning of a line in all the regular files in /etc?
3. How would one search for the word “smtp” at the beginning of a line in all the regular files in /etc and its subdirectories?
4. Explain what the following regular expressions match (all of them are tricky)?
mary.*little.*lamb
one? or two
lil*brother
[0-9]*.[0-9]*.[0-9]*.[0-9]*
^ euro to $
$ to euro ^
R[^0-9]*.00
5. Using sed, how can one search for the phrase “big brother” or “big sister” in an input stream, and replace the “big” with “little”?

Answers to quiz questions

1. sed, grep and many others.
2. grep “^ftp” /etc/* 2>/dev/null (send errors to /dev/null, since /etc is full of directories)
3. Here's how
grep ^[^#] /etc/inetd.conf
Pedantic persons may prefer this answer, which will match blank lines too:
grep '^\([^#]\|$\)' /etc/inetd.conf # yow!
egrep '^([^#]|$)' /etc/inetd.conf # simpler, but uses egrep
4. Here you go
mary.*little.*lamb "mary had a little lamb"
one? or two "one or two" or "on or two"
lil*brother "lilbrother", "lilllbrother" and "librother"
[0-9]*.[0-9]*.[0-9]*.[0-9]*      any four characters
^ euro to $      A line saying " euro to "
$ to euro ^      Nothing
R[^0-9]*.00     "Rx00" or "Ranynondigits?00"
5. echo "big brother" | sed 's/big \(brother\|sister\)/little \1/'

echo "big brother" | sed 's/big \(brother\|sister\)/little \1/'
little brother