我想在终端中查看文本(可以是文件或管道命令输出),但我想用颜色突出显示文本中的特定模式,类似于如何grep
在其输出行中突出显示模式匹配。
我不能在这里使用标准的原因grep "pattern" /path/to/file
是因为我想要查看整个文本(理想情况下是可滚动的,就像通过管道传输一样less
),而不仅仅是包含模式的行。
答案1
答案2
如果你没有被困在颜色突出显示,你可以使用less
它本身例如
less +g -p PATTERN file
或者
less +g +/PATTERN file
突出显示中+/PATTERN
的所有实例,并抑制滚动到第一个匹配的默认行为。PATTERN
file
+g
答案3
用您选择的颜色突出显示多个图案、短语:
您可以将以下脚本保存在名为的文件中~/.rc_highlight_pattern_v0.4_rc
并使用其获取来源source ~/.rc_highlight_pattern_v0.4_rc
(或者您可以编辑~/.bashrc
文件以包含此来源)。
获取此脚本将使_Highlight_Pattern
终端发挥作用。请参阅_Highlight_Pattern -h
获取帮助。
## Script name: rc_highlight_pattern_v0.4_rc
## Script version: 0.4
## Script stats:
## * No. of global variables ( 0* ) : { * there are three, but uset from scope at end }
## 3 (global destructed: __USR_shHIGHLIGHT_PATTERN_ENABLE ; __USR_shHIGHLIGHT_PATTERN_SHOW_INFO ; __USR_shHIGHLIGHT_PATTERN__SCRIPT_ARGS )
## + 1 ( shell variables: REPLY )
## * No. of external commands (which are not among Bash builtins): 1
## * List of external commands: `sed'.
declare -g __USR_shHIGHLIGHT_PATTERN_ENABLE="YES"
declare __USR_shHIGHLIGHT_PATTERN__SCRIPT_ARGS=""
while [[ ${#} -gt 0 ]]; do
__USR_shHIGHLIGHT_PATTERN__SCRIPT_ARGS="${1}"
case "${__USR_shHIGHLIGHT_PATTERN__SCRIPT_ARGS}" in
-i|--info)
__USR_shHIGHLIGHT_PATTERN_SHOW_INFO="YES"
shift
;;
-n|--no-source)
__USR_shHIGHLIGHT_PATTERN_ENABLE="NO"
shift
;;
*)
break ## stop parsing command-line
;;
esac
done
unset __USR_shHIGHLIGHT_PATTERN__SCRIPT_ARGS
if [[ "${__USR_shHIGHLIGHT_PATTERN_SHOW_INFO}" == "YES" ]] ; then
while read -r ; do
echo "${REPLY}"
done <<-__EOF
Description:
This script will avail _Highlight_Pattern() function which can be used to
highlight patterns in the text being written to the stdout.
__EOF
fi
if [[ "${__USR_shHIGHLIGHT_PATTERN_ENABLE}" != "YES" ]] ; then
return 0
fi
#### Definitions start:
function _Highlight_Pattern() {
declare hl_color="red"
declare -i case_insensitive=0
declare output=""
declare -A __color
declare color_start=""
declare color_end=""
__color["black"]='\\033\[1;30m'
__color["red"]='\\033\[1;31m'
__color["green"]='\\033\[1;32m'
__color["brown"]='\\033\[1;33m'
__color["blue"]='\\033\[1;34m'
__color["magenta"]='\\033\[1;35m'
__color["cyan"]='\\033\[1;36m'
__color["white"]='\\033\[1;37m'
__color["nocolor"]='\\033\[0m'
color_end="${__color["nocolor"]}"
declare func_args=""
while [[ ${#} -gt 0 ]]; do
func_args="${1}"
case "${func_args}" in
-c|--color)
hl_color="${2}"
shift # shift past option
shift # shift past arg
;;
-i)
case_insensitive=1
shift # shift past option
;;
--)
shift
break ## stop parsing command-line
;;
-h|--help)
shift
while read -r ; do
echo "${REPLY}"
done <<-__EOF
Usage:
${FUNCNAME} [Options] [PATTERN]
Description: This function highlights the PATTERN in its input which is the
output of another command piped to this function.
Options:
-c, --color color_name Use 'color_name' to highlight pattern. If not used,
the default color will be used. The available choices for 'color_name'
are: black, red, green, brown, blue, magenta, cyan, white. The bright
variant of the color will be used.
-i Use case insensitive search.
-h, --help Show this help.
Limitations: This function is not well tested and may fail at times; this is
experimental.
Examples:
\`echo "A function to highlight pattern." | ${FUNCNAME} -c white -- highlight'
\`echo "A function to highlight pattern." | ${FUNCNAME} highlight'
__EOF
return 0
;;
*)
break ## stop parsing command-line
;;
esac
done
color_start="${__color[${hl_color}]}"
if [[ -z "${color_start}" ]] ; then
color_start="${__color["red"]}"
fi
if [[ ${case_insensitive} -eq 0 ]] ; then
output="$( sed -r 's,('"${1}"'),'"${color_start}"'\1'"${color_end}"',g' )"
shift
while [[ ${#} -gt 0 ]] ; do
output="$( echo "${output}" | sed -r 's,('"${1}"'),'"${color_start}"'\1'"${color_end}"',g' )"
shift
done
else
output="$( sed -r 's,('"${1}"'),'"${color_start}"'\1'"${color_end}"',ig' )"
shift
while [[ ${#} -gt 0 ]] ; do
output="$( echo "${output}" | sed -r 's,('"${1}"'),'"${color_start}"'\1'"${color_end}"',ig' )"
shift
done
fi
echo -e "${output}"
}
unset __USR_shHIGHLIGHT_PATTERN_SHOW_INFO
unset __USR_shHIGHLIGHT_PATTERN_ENABLE
## End Of Script.
使用示例:
echo "Highlight words with colour of your choice, e.g. green." | _Highlight_Pattern -c green green
echo "A function to highlight word." | _Highlight_Pattern word
echo "A function to highlight phrase." | _Highlight_Pattern 'highlight phrase'
echo "A function to highlight pattern." | _Highlight_Pattern 'fun.*pattern'
echo "A function to highlight multiple words of your choice." | _Highlight_Pattern function highlight words
echo "Highlight multiple patterns with _Highlight_Pattern function." | _Highlight_Pattern 'H[^[:space:]]*' 'pa[^[:space:]]*'
echo "Use CaSe-Insensitive search." | _Highlight_Pattern -i 'case-insensitive'
PAGER= man bash | _Highlight_Pattern -i bash | less -R
截屏: