Saturday, May 5, 2012

grep with regular expressions

If you are famliar with the "grep" command for searhching for line containig a particular character sequence in a file or filtering a command output like "ls" or "ps" you may found out that sometimes just passing to grep a plain string is insufficient for you.
There is a more advanced way for searching patterns by using regular expresions.

Here is a simple script i used to learn the basics abaout how regular expressions are used along with grep:

regexp/regex.sh
#!/bin/bash

echo -e {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}{0,1,2,3,4,5,6,7,8,9,A,B,C,
D,E,F}{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}"\n" | sed /^$/d | sed s/^\ // > hex

# lines containig AA somewhere
echo "lines containig AAA somewhere"
echo "beginning with no 1,3,5,7,9,C,D or E and ending with no 0-5"
grep --color=auto -n -E '^[^13579CDE]?AAA[^012345]?$' hex

# lines starting with AAA and contain no digit
echo -e "\nlines starting with AAA and contain no digit at the end"
grep --color=auto -n -E '^AAA[^[:digit:]]' hex

# lines starting with 3-5 and ending with BBB
echo -e "\nlines ending with BBB"
grep --color=auto -n -E '[3-5]BBB$' hex

# lines beginning with no less than two 1 and no more than three 1
# followed by at least 1 D or more
echo -e "\nlines beginning with no less than two 1 and no more than three 1\nfollowed by at least 1 
D or more"
grep --color=auto -n -E '^1{2,3}D+' hex

# lines starting either with 3BB or 3CF ending with 6 to 9
echo -e "\nlines starting either with 3BB or 3CF"
grep --color=auto -n -E '^3(BB|CF)[6-9]' hex

# lines with 4 or 6 only
echo -e "\nlines with 4 or 6 only"
grep --color=auto -n -E '[4]{4}|[6]{4}' hex

exit 0 

The script creates a file with one 16-bit hexadecimal number per line from 0000 to FFFF and performs several grep commands on it.
Options i used for grep were "-n" for showing the line numbers (green on screenshot) and "-E" to use extended regular expressions.
Grep with "-E" option should behave the same way as "egrep" does.

Running the script throws following output to the terminal:


There are a few more lines that didn't fit on the screenshot.

No comments:

Post a Comment