Skip to main content
Rollback to Revision 2
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

#Edit

This is a compromise between what I had originally and glenn's suggestion using the find command (which is much, much slower unfortunately):

search() {
    local file_types grep_cmd opt OPTARG OPTIND pattern start_time stop_time usage
    
    start_time=$(date +%s)
    
    usage="Usage: search [OPTION] ... PATTERN [FILE] ...
Search for PATTERN in each FILE.
Example: search -t c -t h 'hello world' /code/internal/dev/ /code/public/dev/

Output control:
  -t    limit results to files of type"
    
    if [[ $1 == --help ]]; then
        echo "$usage"
        return
    fi
    
    file_types=()
    while getopts ":t:" opt; do
        case $opt in
            t)
                file_types+=("$OPTARG")
                ;;
            ?)
                echo "$usage"
                return
                ;;
        esac
    done
    shift $((OPTIND-1))
    
    if (( $# == 0 )); then
        echo "$usage"
        return
    fi
    
    pattern="$1"
    shift
    
    if (( $# == 0 )); then
        echo "$usage"
        return
    fi
    
    if (( ${#file_types[@]} > 0 )); then
        file_types="${file_types[@]}"
        file_types=.${file_types// /':\|.'}:
        
        grep -I -r "$pattern" "$@" | grep "$file_types"
    else
        grep -I -r "$pattern" "$@"
    fi

    stop_time=$(date +%s)
    echo "Elapsed (seconds):" $(( stop_time - start_time ))
}

I'm not sure if I need to declare all those variables locally at the top of the function, but it seems like a good precaution.

#Edit

This is a compromise between what I had originally and glenn's suggestion using the find command (which is much, much slower unfortunately):

search() {
    local file_types grep_cmd opt OPTARG OPTIND pattern start_time stop_time usage
    
    start_time=$(date +%s)
    
    usage="Usage: search [OPTION] ... PATTERN [FILE] ...
Search for PATTERN in each FILE.
Example: search -t c -t h 'hello world' /code/internal/dev/ /code/public/dev/

Output control:
  -t    limit results to files of type"
    
    if [[ $1 == --help ]]; then
        echo "$usage"
        return
    fi
    
    file_types=()
    while getopts ":t:" opt; do
        case $opt in
            t)
                file_types+=("$OPTARG")
                ;;
            ?)
                echo "$usage"
                return
                ;;
        esac
    done
    shift $((OPTIND-1))
    
    if (( $# == 0 )); then
        echo "$usage"
        return
    fi
    
    pattern="$1"
    shift
    
    if (( $# == 0 )); then
        echo "$usage"
        return
    fi
    
    if (( ${#file_types[@]} > 0 )); then
        file_types="${file_types[@]}"
        file_types=.${file_types// /':\|.'}:
        
        grep -I -r "$pattern" "$@" | grep "$file_types"
    else
        grep -I -r "$pattern" "$@"
    fi

    stop_time=$(date +%s)
    echo "Elapsed (seconds):" $(( stop_time - start_time ))
}

I'm not sure if I need to declare all those variables locally at the top of the function, but it seems like a good precaution.

added 1848 characters in body
Source Link

#Edit

This is a compromise between what I had originally and glenn's suggestion using the find command (which is much, much slower unfortunately):

search() {
    local file_types grep_cmd opt OPTARG OPTIND pattern start_time stop_time usage
    
    start_time=$(date +%s)
    
    usage="Usage: search [OPTION] ... PATTERN [FILE] ...
Search for PATTERN in each FILE.
Example: search -t c -t h 'hello world' /code/internal/dev/ /code/public/dev/

Output control:
  -t    limit results to files of type"
    
    if [[ $1 == --help ]]; then
        echo "$usage"
        return
    fi
    
    file_types=()
    while getopts ":t:" opt; do
        case $opt in
            t)
                file_types+=("$OPTARG")
                ;;
            ?)
                echo "$usage"
                return
                ;;
        esac
    done
    shift $((OPTIND-1))
    
    if (( $# == 0 )); then
        echo "$usage"
        return
    fi
    
    pattern="$1"
    shift
    
    if (( $# == 0 )); then
        echo "$usage"
        return
    fi
    
    if (( ${#file_types[@]} > 0 )); then
        file_types="${file_types[@]}"
        file_types=.${file_types// /':\|.'}:
        
        grep -I -r "$pattern" "$@" | grep "$file_types"
    else
        grep -I -r "$pattern" "$@"
    fi

    stop_time=$(date +%s)
    echo "Elapsed (seconds):" $(( stop_time - start_time ))
}

I'm not sure if I need to declare all those variables locally at the top of the function, but it seems like a good precaution.

#Edit

This is a compromise between what I had originally and glenn's suggestion using the find command (which is much, much slower unfortunately):

search() {
    local file_types grep_cmd opt OPTARG OPTIND pattern start_time stop_time usage
    
    start_time=$(date +%s)
    
    usage="Usage: search [OPTION] ... PATTERN [FILE] ...
Search for PATTERN in each FILE.
Example: search -t c -t h 'hello world' /code/internal/dev/ /code/public/dev/

Output control:
  -t    limit results to files of type"
    
    if [[ $1 == --help ]]; then
        echo "$usage"
        return
    fi
    
    file_types=()
    while getopts ":t:" opt; do
        case $opt in
            t)
                file_types+=("$OPTARG")
                ;;
            ?)
                echo "$usage"
                return
                ;;
        esac
    done
    shift $((OPTIND-1))
    
    if (( $# == 0 )); then
        echo "$usage"
        return
    fi
    
    pattern="$1"
    shift
    
    if (( $# == 0 )); then
        echo "$usage"
        return
    fi
    
    if (( ${#file_types[@]} > 0 )); then
        file_types="${file_types[@]}"
        file_types=.${file_types// /':\|.'}:
        
        grep -I -r "$pattern" "$@" | grep "$file_types"
    else
        grep -I -r "$pattern" "$@"
    fi

    stop_time=$(date +%s)
    echo "Elapsed (seconds):" $(( stop_time - start_time ))
}

I'm not sure if I need to declare all those variables locally at the top of the function, but it seems like a good precaution.

added 1 character in body
Source Link
grep -I -r FooBar /code/internal/dev/ /code/public/dev/ /code/tools/ | grep .c:\|.h:
grep -I -r FooBar /code/internal/dev/ /code/public/dev/ /code/tools/ | grep .c:\|.h
grep -I -r FooBar /code/internal/dev/ /code/public/dev/ /code/tools/ | grep .c:\|.h:
Source Link
Loading