From 2c693a8655eb9eb5c68c21735d4b38dd8eacf362 Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Mon, 23 Feb 2026 23:48:56 -0500 Subject: [PATCH] improvement --- brc | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/brc b/brc index 1793d64..8a7daeb 100644 --- a/brc +++ b/brc @@ -1582,7 +1582,7 @@ etailbg() { ta /var/log/exim4/mainlog /var/log/exim4/*main /var/log/exim4/paniclog /var/log/exim4/*panic -n 200 "$@" ngreset } - +# shellcheck disable=SC2120 etail() { tail -F /var/log/exim4/mainlog /var/log/exim4/*main /var/log/exim4/paniclog /var/log/exim4/*panic -n 200 "$@" } @@ -2340,10 +2340,38 @@ lower() { # make first letter of filenames lowercase. done } - -k() { # history search - grep -iP --binary-files=text "$@" ${HISTFILE:-~/.bash_history} | tail -n 80 || [[ $? == 1 ]]; +# history search +k() { + local grep_out1 grep_out2 histf1 histf2 + local -a lines1 lines2 + histf1=${HISTFILE:-~/.bash_history} + grep_out1=$(grep -iP --binary-files=text "$@" $histf1 | tail -n 80) || [[ $? == 1 ]] + if [[ $EUID == 0 ]]; then + histf2="/home/iank/${histf1##*/}" + grep_out2=$(grep -iP --binary-files=text "$@" $histf2 | tail -n 80) || [[ $? == 1 ]] + elif sudo -nv 2>/dev/null; then + histf2="/root/${histf1##*/}" + grep_out2=$(sudo grep -iP --binary-files=text "$@" $histf2 | tail -n 80) || [[ $? == 1 ]] + fi + if [[ $grep_out1 && $grep_out2 ]]; then + mapfile -t lines1 <<<"$grep_out1" + mapfile -t lines2 <<<"$grep_out2" + # in this case, just insert a chunk of secondary results in the middle. + if (( ${#lines1[@]} > 20 )); then + printf "%s\n" "${lines1[@]:20}"; printf "############ ^ %s #############\n" $histf1 + printf "%s\n" "${lines2[@]::20}"; printf "############ ^ %s #############\n" $histf2 + printf "%s\n" "${lines1[@]::20}" + else + printf "%s\n" "${lines2[@]}"; printf "########## ^ %s #########\n" $histf2; + printf "%s\n" "${lines1[@]}" + fi + elif [[ $grep_out2 ]]; then + printf "%s\n" "${lines2[@]}"; printf "### ^ %s\n" $histf2 + elif [[ $grep_out1 ]]; then + printf "%s\n" "${lines1[@]}" + fi } + ks() { # history search with context # args are an extended regex used by sed history | sed -nr "h;s/^\s*(\S+\s+){4}//;/$*/{g;p}" | tail -n 80 || [[ $? == 1 ]]; -- 2.30.2