繁体   English   中英

通过bash在tput结果上使用column命令

[英]Using column command on tput result over bash

我有一个变量“ x”,它包含两列和两行。 我想以红色打印“ hi”,因此我在tput帮助下将结果打印为红色。 但是我还需要为使​​用column -t的列以正确的对齐方式打印列,但这会使输出失真。 这是因为事实是,通过tput添加了一些控制字符。

x="hello $(tput setaf 1)hi $(tput sgr0) whatsup
hey howdy cya"


echo "$x"
hello hi  whatsup
hey howdy cya

echo "$x"|column -t
hello  hi              whatsup
hey    howdy  cya

我期待:

hello  hi     whatsup
hey    howdy  cya

尝试调试,发现tput正在添加一些控制字符以使“ hi”打印为红色。

echo "$x"|cat -A
hello ^[[31mhi ^[(B^[[m whatsup$
hey howdy cya$

题:

如何在tput的彩色输出上“ column -t ”?

编辑:@Diego Torres Milano的结果(全部为红色)

hello  31mhi  Bm  whatsup
hey    howdy   cya

您可以使用一种简化的标记 ,在这种情况下,您的红色为^A (使用vim类型CTRL + v CTRL + a来输入)

y="hello ^Ahi whatsup
hey howdy ya"

echo "$y"|column -t|sed -E "s@^A([[:alnum:]]+)@$(tput setaf 1)\1$(tput sgr0)@g"

并且输出是预期的(hi为红色):

hello  hi     whatsup
hey    howdy  ya

编辑

如果您的column包含控制字符,则使用值中未出现的任何字符,然后替换它们,例如

y="|hello !hi |whatsup
|hey |howdy |ya"

echo "$y"|column -t|sed -E "s@\\|@@g; s@!([[:alnum:]]+)@$(tput setaf 1)\1$(tput sgr0)@g;"

产生

列颜色

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM