我编程&QUOT即可;在燃烧" ANSI控制codeS使用UNIX utils的文件? [英] Can I programmatically "burn in" ANSI control codes to a file using unix utils?
问题描述
例如:我开始与剧本
录音,并尝试键入 echo测试
但省略了0,所以我退格来纠正它。
Example: I start recording with script
, and try to type echo test
but omit the o, so I backspace to correct it.
当我猫打字稿
一切看起来都很正常,因为codeS是相互preTED,但如果我用少
或 VIM
我看到 ECH测试^ H ^ [[K ^ H ^ [[K ^ H ^ [[K ^ H ^ [[K ^ H ^ [柯测试^ M
When I cat typescript
everything looks normal, since the codes are interpreted, but if I use less
or vim
I see ech test^H^[[K^H^[[K^H^[[K^H^[[K^H^[[Ko test^M
我完全理解这是什么,为什么它的发生,但没有任何办法codeS为烧伤,只是看到结果在一个文件?我的缺憾方法是猫
的文件,然后复制/粘贴文本出了码头,但肯定猫的某种组合,用sed,awk的,还是其他什么东西可以让我还有更容易?
I fully understand what this is and why it's happening, but is there any way to "burn in" the codes and just see the result in a file? My kludgy method is to cat
the file, then copy/paste the text out of the terminal, but surely some combination of cat, sed, awk, or something else can get me there more easily?
推荐答案
要显示包含ANSI序列的文件,
To display a file that contains ANSI sequences,
less -r typescript
或者
less -R typescript
从文件中删除ANSI和退格序列,创造一个干净的的newfile
,请尝试:
sed -r ':again; s/[^\x08]\x08\x1b\[K//; t again; s/\x1b_[^\x1b]*\x1b[\]//g; s/\x1B\[[^m]*m//g' typescript >newfile
工作原理
-
-r
本打开扩展的正前pressions。 (在BSD系统,
-r
应与-E
替换。GNU SED现代版本将接受-r
或-E
)This turns on extended regular expressions. (On BSD systems,
-r
should be replaced with-E
. Modern versions of GNU sed will accept either-r
or-E
.)`:一次; S / [^ \\ X08] \\ X08 \\ X1B [K //;再牛逼
`:again; s/[^\x08]\x08\x1b[K//; t again
这将消除任何退格序列。这些都是在一个循环的时间内完成的。
This removes any backspace sequences. These are done one at a time in a loop.
S / \\ X1B _ [^ \\ X1B] * \\ X1B [\\] //摹
作为
的xterm
扩展(见的文档),Esc键_东西Esc键\\
会做什么。此命令将删除这些序列。As an
xterm
extension (see documentation),Esc _ something Esc \
will do nothing. This command removes these sequences.S / \\ X1B \\ [[^ M] * M //摹
这其中除去颜色设置,其余ANSI序列等。
This removes the remaining ANSI sequences which set colors, etc.
这涵盖了所有,我通常碰到的控制序列。有各种各样的扩展控制序列,如果您的输出有一些,我没有看到,code可能需要延长。
This covers all the control sequences that I normally run into. There are a wide variety of extended control sequences and, if your output has some that I haven't seen, the code may need to be extended.
在一个BSD或者POSIX系统,单独的命令必须以
-e
链接在一起的选项,而不是分号。因此,请尝试:On a BSD or POSIX system, individual commands have to be chained together with
-e
options instead of semicolons. Thus, try:sed -e ':again' -e 's/[^\x08]\x08\x1b\[K//' -e 't again' -e 's/\x1b_[^\x1b]*\x1b[\]//g' -e 's/\x1B\[[^m]*m//g'
这篇关于我编程&QUOT即可;在燃烧" ANSI控制codeS使用UNIX utils的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!