使用bash从文本文件中删除ANSI颜色代码 [英] Remove ANSI color codes from a text file using bash

查看:256
本文介绍了使用bash从文本文件中删除ANSI颜色代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以运行并输出到文本文件的bash脚本,但是它还包含了它所使用的颜色代码,我想知道如何从文件中删除它们,即

I have a bash script that runs and outputs to a text file however the colour codes it uses are also included what i'd like to know is how to remove them from the file, ie

^[[38;1;32mHello^[[39m
^[[38;1;31mUser^[[39m

所以我只想留下Hello和User

so I just want to be left with Hello and User

推荐答案

sed -r "s/\x1B\[(([0-9]{1,2})?(;)?([0-9]{1,2})?)?[m,K,H,f,J]//g" file_name

此命令从文件中删除特殊字符和颜色代码

this command removes the special characters and color codes from the file

这些是一些ANSI代码: ESC[#;#H or ESC[#;#f将光标移至行#的列# ESC[2J清除屏幕和主光标 ESC[K清除到行尾,

these are some of ANSI codes: ESC[#;#H or ESC[#;#f moves cursor to line #, column # ESC[2J clear screen and home cursor ESC[K clear to end of line,

注意,如果代码清晰,则既没有数字也没有分号;

note in case of clear code there is neither number nor semicolon ;

同意以下评论: 如果数字多于2位,请使用:

agree with below comment: if the numbers are more than 2 digit kindly use this:

sed -r "s/\x1B\[(([0-9]+)(;[0-9]+)*)?[m,K,H,f,J]//g" filename

这篇关于使用bash从文本文件中删除ANSI颜色代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆