March 22, 2017

Grep

Grep is a great powerful tool that print lines matching certain patterns.

Some example Regular Expressions using Grep:

Remove BOTs from the website access.log file:

grep -Ev '|spider|slurp|yandex|bingbot|majestic12' access.log > access2.log

Show only City/Towns in Massachusetts that have at least 19 charaters

grep -x '.{10}' MassachusettsCityTowns.txt

Highlight the Search Results

export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
grep '.{10}' MassachusettsCityTowns.txt

Using Grep in Bash Script

This is a script that I wrote a long time ago, that I used to check to see how many times a certain error would appear in a log file.

grepcode

#!/bin/bash
itoday=$(date +"%d/%b/%Y")
todayerrors=$(cat grep $itoday /server/debug.log | grep $itoday | grep "Exception" | wc -l)
echo "# ------------------------------------------------------------"
echo "# Number of Errors Found on $itoday : $todayerrors"
echo "# ------------------------------------------------------------"
echo "# Raw data"
grep -C 10 "Exception" /mnt/$i/server/debug.log

This script will count out the total number of errors that were generated today. Then display the RAW lines. This is useful to have handy to see if a particular error is occurring quite often.

Quick RegEx Table

This is a quick regular expression table that I found many years ago. It's quick reminder of some of the common grep commands:

#######################################################
# RegExpression Table
#######################################################
Reg-expr   | Description
-----------+---------------------------------------------------
.          | Matches any character except newline
[a-z0-9]   | Matches any single character of the set
[^a-z0-9]  | Matches any single character not in set
d         | Matches a digit,                   i.e., [0-9]
w         | Matches a alpha-numeric character, i.e., [a-zA-Z0-9_]
W         | Matches a non-word character,      i.e., [^a-zA-Z0-9_]
metachar  | Matches the character itself,      i.e., |, *, +
x?         | Matches 0 or 1 x's, where x is any of the above
x*         | Matches 0 or more x's
x+         | Matches 1 or more x's
x{m,n}     | Matches at least m x's but no more than n
foo|bar    | Matches one of foo or bar
(x)        | Brackets a regular expression (this is a bit of a lie :-)
b         | Matches a word boundary

Comments

Add your Comments

Feel free to leave a comment about this post.

- Feel Free to add HTML to your comment!