【问题标题】:Use sed to add background color to tabs?使用 sed 为选项卡添加背景颜色?
【发布时间】:2011-04-07 01:58:40
【问题描述】:

是否可以使用 sed 来更改选项卡(或任何其他文本)的背景颜色,例如,我可以运行以下内容?

somefunction | sed -e 's/(some pattern)/(set bg color)\1(unset bg color)/g'

【问题讨论】:

  • 是的,这是可能的。我希望有人过来给你答案,因为我现在不在电脑旁,我不记得我是怎么做到的。不过,与终端控制代码有关。

标签: linux unix sed


【解决方案1】:

是的:

#!/bin/bash

norm_bg=$(tput sgr0)
red_bg=$(tput setab 1)

echo -e "foo\tbar\tbaz" | sed "s/\t/$red_bg    $norm_bg/g"

其他Color Codes(最底部)请参阅此链接

【讨论】:

    【解决方案2】:

    您可以直接在 sed 脚本中插入着色:

    echo -e "foo\tbar\tbaz" | sed 's/\t/\o033[41m    \o033[0m/g'
    

    说明:

    • 将背景设置为红色 = \o033[41m\x1B[41m\c[[41m
    • 重置颜色 = \o033[0m\x1B[0m\c[[0m

    您也可以阅读类似的问题/anwsers:
    Escaping the '\' character in the replacement string in a sed expression

    有关颜色代码的更多信息,请参阅Arch wiki

    【讨论】:

      【解决方案3】:

      为 olibre +1。

      这也适用于 osx 和 linux、bash 和 zsh,使用 extquote

      echo My name is Chad. | sed -e 's/Chad/\'$'\033[31m&\033[(B\033[m/'
      

      【讨论】:

      • +1 也适合你 ;-) 在这种情况下使用 extquote 是个好主意:你不必记住特定的 sed 处理特殊字符的方法。我会重复使用你的小费 ;-) 干杯
      猜你喜欢
      • 2013-06-13
      • 2013-02-25
      • 2012-08-08
      • 2016-07-26
      • 1970-01-01
      • 2014-05-07
      • 2017-04-24
      • 2019-08-06
      • 2020-05-11
      相关资源
      最近更新 更多