All pastes #674072 Raw Edit

/BIN/find.sh

public text v1 · immutable
#674072 ·published 2007-08-28 22:08 UTC
rendered paste body
#!/bin/bash

# find in files
if [ $# -eq 0 ]; then echo "usage: $0 \"<dir(s)>\" \"<filename pattern (*.java)>\" \"<searchstring(s)>\" [-verbose]"; exit 1; fi

dirs=$1;
filepattern=$2;
searches=$3;
option=$4;

cnt=0;
for d in $dirs; do
  tmp=$(mktemp -p /tmp XXXXXX); rm -f $tmp; tmp="_"${tmp//\//}${tmp//\//}tmp"_"; # echo $tmp; # make random string (YY6Qjj), then change to _tmpYY6QjjtmpYY6Qjjtmp_
  files=$(find $d -type f -name "$filepattern" | sed -e "s/ /$tmp/g"); # echo $files; # get files and apply temp string replacement for " " to keep files w/ spaces in names together
  for f in $files; do
    g=${f//$tmp/ }; # echo "$g ..."; # remove temp string replacement for " "
    if [ "$(egrep -c "$searches" "$g")" != "0" ]; then
      (( cnt++ ));
      echo "** [$cnt] $f ** ";
      if [ "$option" = "-V" ] || [ "$option" = "-verbose" ]; then
        egrep -n -B2 -A2 "$searches" "$g";
        echo "";
      fi
    fi
  done
done