#!/usr/bin/perl -w # if [ -z "$1" ]; then # echo 'Usage: 02grep_levels.sh []+' # echo ' Greps all levels containing all s and create index_test.txt for these levels' # else # PWD=`pwd` # cd $ENIGMA/data/levels # REGEX=`echo "$*" | sed -e 's/ /\\\\\&/ig' ` # echo "REGEX='$REGEX'" # LEVELS=`grep -i "\\($REGEX\\)" *.lua */*.lua | sed -e "s/:.*//ig" | sort | uniq | \ # perl -e 'my @a=map { chomp; s/\.lua//ig; "$_"; } ; \ # foreach (@a) { print "^$_\\\\|"; } \ # print "sdsdjws7d67sd7"; ' \ # ` # # echo "'$LEVELS'" # INDEX=`grep -hi "\\($LEVELS\\)" index_*.txt` # echo "$INDEX" >index_test.txt # echo index_test.txt has been updated. # cd $PWD # fi my $regCount = $#ARGV+1; my $maxHits = 0; if ($regCount le 0) { print "Usage: 02grep_levels.pl []+\n"; print " Greps all levels containing all s and create index_test.txt for these levels\n"; exit 1; } my %levels = (); my %maxHittenLevels = (); foreach(@ARGV) { my @found = `grep -i '$_' *.lua */*.lua | sed -e "s/:.*//ig" | sort | uniq`; # print "------------------------------------------------------------\n"; # print "Found for '$_'\n"; # print @found; foreach (@found) { chomp; if (exists $levels{$_}) { $levels{$_}++; } else { $levels{$_} = 1; } if ($levels{$_} > $maxHits) { $maxHits = $levels{$_}; } } } # print "------------------------------------------------------------\n"; # print "Summary:\n"; foreach (keys %levels) { # print "$levels{$_} $_\n"; if ($levels{$_}==$maxHits) { $maxHittenLevels{$_}=$maxHits; } } # print "------------------------------------------------------------\n"; if ($maxHits == 0) { print "No regex matched.\n"; } elsif ($maxHits<$regCount) { print "No level matched all $regCount expressions.\n"; print "Saving levels that were hit by at least $maxHits expressions.\n"; } my $index_regex = ''; foreach (keys %maxHittenLevels) { # print "hit: $_\n"; s/\.lua//ig; $index_regex .= "\\|^$_"; } $index_regex = '\\('.substr($index_regex,2).'\\)'; # print "index_regex='$index_regex'\n"; my $outname = 'index_test.txt'; open FILE, ">$outname" || die "Can't open '$outname'"; print FILE `grep -hi '$index_regex' index_*.txt`; close FILE; my $hits = scalar(keys %maxHittenLevels); print "$hits levels indexed into '$outname'\n";