Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'A simple script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A simple script
Authored by: SlewSys on Feb 24, '05 02:11:09PM
It is faster to use find(1) with xargs(1), but then the syntax is a little more complicated. Here is a script that reduces the syntax to a bare minimum for the most common usage - i.e., finding files containing strings. To use it, copy the text below and save it (e.g., with TextEdit) to a file named `pat' in your current path (.e.g., /usr/bin/pat or preferably someplace like /opt/bin/pat, if that's in your path). Examples of how the command is used are contained in the comments of the script itself. Don't forget to make the script executable, e.g., with the command line:

    $ chmod +x /whatever/path/pat
---- CUT HERE ----

#!/bin/sh -
#
#    @(#)pat
#
# This script greps files matching a pattern under the current folder.
#
# EXAMPLES
#
# To display a list of files under the current directory
# containing the word "hello" ignoring case, use:
#
#       $ pat -li  hello
#
# To display, under the current directory, the actual lines in files
# with the `.txt' extension containing either "hello" or "world":
#
#       $ pat 'hello|world' *.txt
#
PATH=/bin:/usr/bin

USAGE="usage: pat [egrep-options] egrep-pattern [file-glob ... [-- find-args]]"

typeset -i i

i=0
for arg; do
    case "$arg" in
    --) break ;;
    -*) egrep_arg[i++]="$arg" ;;
    *)  egrep_arg[i++]="$arg"
        break ;;
    esac
done

(( i>0 )) || { echo "$USAGE" >&2; exit 2; }
shift $i 

find_arg[i=0]="-type f"
for arg; do
    case "$arg" in
    --) (( ++i ))
        break ;;
    *)  if (( i==0 )); then
          find_arg[i++]="-a \( -name '$arg'"
        else
          find_arg[i++]="-or -name '$arg'"
        fi ;;
    esac
done

(( i>0 )) && find_arg[i]="\)" 
shift $i

eval find . "${find_arg[@]}" $@ -print0 | xargs -0 -n30 egrep "${egrep_arg[@]}"


[ Reply to This | # ]
A simple script
Authored by: cxd101 on Nov 15, '05 12:57:10PM

Thanks so much! I was looking for a 'UNIX' way to do this for some time.



[ Reply to This | # ]