From 98c94912e185babc6ba6e949c5fa1f30b5a2c1d2 Mon Sep 17 00:00:00 2001 From: korhadris Date: Sat, 22 Aug 2015 23:37:01 -0700 Subject: [PATCH] Replace use of grep -w with grep -x. Prepend "^" to start of latentWhitelist.txt lines. The -x switch requires a full line match of the regexp, where as -w will try to find the match somewhere in the line, looking for work breaks. Combined with turning the whitelist lines into full regexps, this results in significantly faster parsing. Having "^" prepended to the lines also keeps false whitelisting from occuring, such as the following example: If whitelist.txt contains "google.com" it would whitelist many other sites that end in "google.com" as long as there is a non-word character preceeding the google (such as "-", or "."). --- gravity.sh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/gravity.sh b/gravity.sh index 8a6f22d..9bad747 100755 --- a/gravity.sh +++ b/gravity.sh @@ -115,8 +115,10 @@ if [[ -f $whitelist ]];then numberOf=$(cat $whitelist | sed '/^\s*$/d' | wc -l) plural=; [[ "$numberOf" != "1" ]] && plural=s echo "** Whitelisting $numberOf domain${plural}..." - # Append a "$" to the end of each line so it can be parsed out with grep -w - awk -F '[# \t]' 'NF>0&&$1!="" {print $1"$"}' $whitelist > $latentWhitelist + # Append a "$" to the end, prepend a "^" to the beginning, and + # replace "." with "\." of each line to turn each entry into a + # regexp so it can be parsed out with grep -x + awk -F '[# \t]' 'NF>0&&$1!="" {print "^"$1"$"}' $whitelist | sed 's/\./\\./g' > $latentWhitelist else rm $latentWhitelist fi @@ -126,8 +128,9 @@ plural=; [[ "${#sources[@]}" != "1" ]] && plural=s echo "** Whitelisting ${#sources[@]} ad list source${plural}..." for url in ${sources[@]} do - echo "$url" | awk -F '/' '{print $3"$"}' >> $latentWhitelist + echo "$url" | awk -F '/' '{print "^"$3"$"}' | sed 's/\./\\./g' >> $latentWhitelist done -grep -vwf $latentWhitelist $origin/$matter > $origin/$andLight + +grep -vxf $latentWhitelist $origin/$matter > $origin/$andLight gravity_advanced