1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sed 出力をカラー表示

Last updated at Posted at 2018-06-22

こんにちは
sed コマンドの出力をターミナル上でカラー表示させる方法を調べました。下記例で gsed にマッチさせた pp の部分がカラー表示されます1。sed 利用ですので冗長になりました。方法はここで見つけました:

$ echo -e "11pp22\n33qq" | while read line; do esc=$(printf '\033'); echo $line | gsed -e "s,\([0-9]\+\)\([a-z]\+\)\([0-9]\+\)$,\1${esc}[32m\2${esc}[0m\3,"; done
11pp22
33qq

egrep

また、egrep コマンドで行頭部でマッチさせた部分を同様にカラー表示することができます(下記例で 111a の部分)。この方法はコメントでいただきました。

$ echo -e "111a222\n333" | GREP_COLOR='01;32' grep -E --color '^|\d+a'
111a222
333
$
  1. この例のコード番号 32 は foreground color の green です(see ANSI escape code#Colors (Wikipedia))。 ↩

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?