是否有可能“查色”输出?
我有一个发现,search/所有我的服务器,并exec'rm'某些文件。 因为这些与我的其他发现结果混合在一起,除此之外,我想为它们着色。
这可能吗?
使用GNU查找,您可以使用-printf而不是-print来自定义打印文件名的方式。 (您也可以使用标准查找来执行此操作,但是可以通过-exec sh -c 'echo ...' {} )以循环方式-exec sh -c 'echo ...' {} 。例如,以下命令以绿色和其他常规文件以默认颜色:
find . -type f \( -perm +100 -printf '\033[32m%p\033[0m\n' -or -print \)
我通常用来突出滚动命令行文本是使用这个bash + perl函数:
highlight() { perl -pe "s/$1/\e[1;31;43m$&\e[0m/g"; }
因此:
command | highlight "desired text"
这与@jrods的答案类似,但是这不需要Perl。 这适用于安装在Darwin,Linux和FreeBSD上的GNU grep。
你可以使用grep和颜色,并通过grep来pipe你的命令:
export GREP_OPTIONS="--color=auto"
然后,要突出显示文本,只需执行以下操作:
find / -name "perl" |grep "your_string_here"
find ... | grep -E --color 'words of interest|more good stuff|$'
美元符号使它匹配每行的结尾,但没有任何亮点,所以它输出偶数行而不匹配,同时突出显示其他事物。
例如,我使用了一个用于replacels -d */ .*/ (别名)的replace方法. 和..目录:
找 。 -mindepth 1 -maxdepth 1 -type d -exec ls –color = auto -d {} \;
通过这种方式,我不仅可以突出显示,而且可以突出显示通常应用的系统。
我正在使用以下函数(在〜/。{ba,z} shrc中定义):
HLCOLOR="\x1b[30;47m" NOCOLOR="\x1b[0m" #Usage: find [dir] [mask1] [mask2]... #where maskN will become <<-iname '*maskN*'>> param for 'find' #'dir' should be an existing directory. otherwise it will be recognized as mask. function findm { local it cl sp; [ -d "$1" ] && cl="'$1'" && shift; for it in "$@"; do cl="${cl} -iname '*${it}*' -or"; sp="${sp}${it}\\|"; done; cl="${cl%-or}"; sp="s/\\(${sp%\\|}\\)/${HLCOLOR}\\0${NOCOLOR}/ig"; eval find ${cl} | sed -e "${sp}"; }
如果grep中的颜色是on或auto,那么可以使用extended-regexp选项和美元符号(仍然显示每一行)简单地执行一个简短的greppipe道。
grep -E 'word|$'
例如,通过查找search进行pipe道连接:
find . -type f | grep -E 'readme|$'
这将突出每一个“自述”的发生,并仍然显示查找的全部输出。
旁注:如果grep有颜色,你可以添加–color选项( grep -E --color ... ),正如Dennis Williamson所build议的那样,或者为session设置( export GREP_OPTIONS="--color=auto" )。