Skip to content

Instantly share code, notes, and snippets.

@ruanhao
Last active November 15, 2017 05:49
Show Gist options
  • Save ruanhao/e94d426f715dcc22e1405b8462685354 to your computer and use it in GitHub Desktop.
Save ruanhao/e94d426f715dcc22e1405b8462685354 to your computer and use it in GitHub Desktop.
search text with multiple patterns and highlights
function m () {
function _usage ()
{
echo "usage: COMMAND [-inHRp] -p<pattern1> -p<pattern2> <filename>";
echo "-i : ignore case";
echo "-n : show line number";
echo "-H : show filename";
echo "-h : show header";
echo "-p : specify pattern";
echo "-R : search recursively"
};
declare -a patterns;
local ignorecase_flag recursive_flag filename linum header_flag colon result OPTIND;
while getopts "iHhnRp:" opt; do
case $opt in
i)
ignorecase_flag=true
;;
H)
filename="FILENAME,"
;;
n)
linum="NR,"
;;
p)
patterns+=("$OPTARG")
;;
h)
header_flag=true
;;
R)
recursive_flag=true
;;
\?)
_usage;
return
;;
esac;
done;
if [[ -n $filename || -n $linum ]]; then
colon="\":\",";
fi;
shift $(( $OPTIND - 1 ));
if [[ $ignorecase_flag == true ]]; then
for s in "${patterns[@]}";
do
result+=" && s~/${s,,}/";
done;
result=${result# && };
result="{s=tolower(\$0)} $result";
else
for s in "${patterns[@]}";
do
result="$result && /$s/";
done;
result=${result# && };
fi;
result+=" { print "$filename$linum$colon"\$0 }";
if [[ ! -t 0 ]]; then
cat - | awk "${result}";
else
for f in "$@";
do
if [[ -d $f ]]; then
if [[ $recursive_flag == true ]]; then
find -L $f -not -path '*/\.*' -type f -print0 2> /dev/null | while IFS= read -r -d '' ff; do
if file "$ff" | grep --color=auto --line-buffered -q "text"; then
[[ $header_flag == true ]] && echo "########## processing $ff ##########";
awk "${result}" "$ff";
fi;
done;
fi;
else
if file "$f" | grep --color=auto --line-buffered -q "text"; then
[[ $header_flag == true ]] && echo "########## processing $f ##########";
awk "${result}" "$f";
fi;
fi;
done;
fi
}
function h () {
function _usage ()
{
echo "usage: YOUR_COMMAND | h [-idn] args...
-i : ignore case
-d : disable regexp
-n : invert colors"
};
local _OPTS OPTIND;
if test -t 0; then
_usage;
return;
fi;
while getopts ":idnQ" opt; do
case $opt in
i)
_OPTS+=" -i "
;;
d)
_OPTS+=" -Q "
;;
n)
n_flag=true
;;
Q)
_OPTS+=" -Q "
;;
\?)
_usage;
return
;;
esac;
done;
shift $(($OPTIND - 1));
if (( ${#@} > 12)); then
echo "Too many terms. h supports a maximum of 12 groups. Consider relying on regular expression supported patterns like \"word1\\|word2\"";
exit -1;
fi;
[[ -n $ZSH_VERSION ]] && setopt localoptions && setopt ksharrays && setopt ignorebraces;
local _i=0;
if [ -z $n_flag ]; then
_COLORS=("underline bold red" "underline bold green" "underline bold yellow" "underline bold blue" "underline bold magenta" "underline bold cyan" "bold on_red" "bold on_green" "bold black on_yellow" "bold on_blue" "bold on_cyan" "bold on_magenta");
else
_COLORS=("bold on_red" "bold on_green" "bold black on_yellow" "bold on_blue" "bold on_magenta" "bold on_cyan" "bold black on_white" "underline bold red" "underline bold green" "underline bold yellow" "underline bold blue" "underline bold magenta");
fi;
for keyword in "$@";
do
local _COMMAND=$_COMMAND"ack $_OPTS --noenv --flush --passthru --color --color-match=\"${_COLORS[$_i]}\" '$keyword' |";
_i=$_i+1;
done;
_COMMAND=${_COMMAND%?};
cat - | eval $_COMMAND
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment