Many times we know that we wrote some code snippet which we want to reuse but cannot remember which file it is in. We need to search inside those python files to get to that code snippet. The following script is devised to make that search easier. It goes into every subdirectory and searches every text file for the string. To get the usage just run the script without any arguments.
#!/bin/bash # search inside text files : v4.1 by Saugata usage=" Usage: ./searchinsidefiles.sh [-s word to search] [-t file types to search] [-v verbose] [-h help] Example: ./searchinsidefiles.sh -s DataFrame -t py Example: ./searchinsidefiles.sh DataFrame py " # ---- SET INITIAL VALUES ---- word="" file_ext="*" verbose=0 # ---- GETOPTS ---- # no args. print usage and exit if [[ $# -eq 0 ]]; then echo "$usage" exit fi # if $1 doesn't start with a switch - then user have used # the other way of passing args if [[ "$1" =~ ^[a-zA-Z0-9]+$ ]]; then # ---- SET INITIAL VALUES ---- word=$1 file_ext=$2 # Second argument might be empty # which means $file_ext # will be empty at this point too #Set the values of $num and $special #to the default values in case they are empty [ "$2" == "" ] && file_ext="*" else # user have used a switch to pass args. Use getopts while getopts s:t:vh option do case "${option}" in s) word=${OPTARG};; t) file_ext=${OPTARG};; v) verbose=1;; h) echo "$usage" exit ;; esac done fi # ----------------------- echo echo "Pattern to search for: " $word echo "Files being searched: " $file_ext echo IFS=$'\n' filenames=`find . -type f -name "*.$file_ext"` for i in $filenames do istextfile=`file $i | grep "text"` if [ "$istextfile" ]; then text=`cat $i | grep "$word"` if [ "$text" ] ; then echo "-------------------------------------" echo "FILE : " $i if [ "$verbose" -eq "1" ] ; then echo grep $word $i -A2 -B2 --color=auto echo fi fi fi done
A python GUI is going to follow.