Page 1 of 1

PBfind: Find sought text in any/all PB source files quickly

Posted: Sat Sep 12, 2015 1:06 pm
by oldefoxx
1. Save it to a convenient place in your account. I picked ~/PBfind.sh. You might prefer ~/Desktop/PBfind.sh.

Note: the choice of name and the added extension of .sh are optional. It will work without the ,sh, but adding the .sh means gedit will colorize keywords in the script when viewed or edited in the future.

After you save the file, you need to make it executable. you can do this from the command line with:

chmod +x ~/PBfind.sh

or

chmod +x ~/Desktop/PBfind.sh

or wherever and whatever you put it and named it.

OR, from the GUI, you right-click on the file icon and then click on Properties. Then click on Properties and check the box marked "Allow executing file as program".

You can also search online for adding a Desktop Launcher to your setup for script files that would make it easier to launch this one going forward. It's not hard to do, but better explained elsewhere.

The script file loops so that you can do new searches repeatedly. The up and down arrows let you explore the results. It checks all folders except two, and all files that match "*.pb*". You can change the expression between the double quotes to check other types of files. You can replace $HOME in the grep statement with "/home/" if you want to search other folders under other accounts as well. When you end the search, use "q" to exit the viewer, and either Ctrl+C or click the X box of the terminal window to stop searches altogether.

All searches are based on 1-line-at-a-time matches. If what you want spans more than one line, i will not be found. When you exit the script file, it leaves two temporary files behind, 0.tmp is the unprocessed results of the grep command. where each line has it all: path/filename:ln#:line content. To view this file well, take the terminal or viewer window to full screen.

The second file, 1.tmp, has the contents of 0.tmp reprocessed for improved readability. The path and filename appear just once, then individual lines from that file that matched shown below, with the line number in front of each.

Code: Select all

L#!/bin/bash
len=${#HOME}                                # Showing how to find the length.  Problem is, this is treated
                                            # as a string, not a number.  Can't find a way to make over into
                                            # into a number.
echo $HOME "has length of" $len "characters."
while :
do
  echo
  read -p "Find what: " what                # Intended to search PureBasic (*.pb?) source files for text matches
  a0=""; > 0.tmp; > 1.tmp
  grep -rHn $what $HOME --include="*.pb*" --exclude-dir=".cache" --exclude-dir=".gvfs" >> 0.tmp
  while read line                           # this checks for and reads the next line
  do										# the closing 'done' has the file to be read appended with "<"
    a1=$(echo $line | awk -F: '{print $1}') # this gets the full path before the first ':'
    a2=${line#$a1":"}     					# renove path and first colon from rest of line
    if [[ $a0 != $a1 ]]
    then
      echo  >> 1.tmp
      echo $a1":" >> 1.tmp
      a0=$a1
    fi
    echo " "$a2 >> 1.tmp
  done < 0.tmp
  cat 1.tmp | less
done
It helps me. Hope it helps you.