本文介绍了彩色Linux命令输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想为locate命令的输出着色,以便轻松将其与其他终端文本区分开.

For example, I'd like to color the output of the locate command so it's easily distinguished from the other terminal text.

它应该像这样工作:

locate -bir pdf | some_command_to_color_the_result

着色不应仅限于locate命令:我需要一种通用的解决方案,该解决方案使用管道为文本着色,例如,将grepcat的输出输入文本.

Coloring shouldn't be limited for the locate command only: I need a generic solution that colors text using pipelines, for example feeding it the output of grep or cat.

如果没有内置的Linux命令,那么任何有关如何创建命令的提示都将受到欢迎.

If there's no built-in Linux command, any hints on how to create one are appreciated.

推荐答案

norm="$(printf '\033[0m')" #returns to "normal"
bold="$(printf '\033[0;1m')" #set bold
red="$(printf '\033[0;31m')" #set red
boldred="$(printf '\033[0;1;31m')" #set bold, and set red.

somecommand | sed -e "s/someregexp/${boldred}&${norm}/g"  # will color any occurence of someregexp in Bold red

printf "%s" "$red" ; locate something ; printf "%s" "$norm"  # will color output of locate something in red
   #I (ab)use printf "%s" "something", as it's more portable than echo,and easy to modify

还有许多其他方式(例如,创建可以使正则表达式着色的函数/脚本,然后:somecommand | colorize -c green 'foo.*bar' 'other')

There are many other ways (create a function/script that can colorize a regexp, for example, and then : somecommand | colorize -c green 'foo.*bar' 'other' )

这篇关于彩色Linux命令输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 01:58