SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2011-10-01 09:27:57

xaos52
The Good Doctor
From: Planet of the @s
Registered: 2011-06-24
Posts: 4,602

How to remove ANSI escape sequences from a text file.

How to remove ANSI escape sequences from a text file: e.g. remove escape sequences from the output of 'script':
I needed this functionality just now and I thought 'Why not share it?'. So, here it is:

Add to your $HOME/.bashrc

alias stresc='sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"'

You may replace 'stresc' by whatever you prefer, just make sure it is not an already existing command or alias.
This effectively creates your own personal 'strip escape' command.
To use it in a terminal

stresc filename
or
stresc filename > other-file-name

I found the sed command here: http://serverfault.com/questions/71285/ … -text-file

hth

Offline

Help fund CrunchBang, donate to the project!

#2 2011-10-01 11:05:42

Awebb
The Singularity
Registered: 2009-07-23
Posts: 2,812

Re: How to remove ANSI escape sequences from a text file.

Your link provides a relevant inquery.

You can't, because what is an escape sequence isn't well-defined in general -- you need to know what sort of terminal your escape sequences are designed for. If you want to restrict the problem to "strip ANSI colour sequences" (a fairly likely assumption), something like:

I generally recommend learning more about sed and regular expression. It made my life easier so many times, but also harder, because regexp is so sensitive to mistakes.


I'm so meta, even this acronym

Offline

#3 2011-10-01 12:29:58

xaos52
The Good Doctor
From: Planet of the @s
Registered: 2011-06-24
Posts: 4,602

Re: How to remove ANSI escape sequences from a text file.

^ OK, that is a valid proviso.
So, in the future, I will first try this command and then, if it does not do what I intended it to do, I will look into the sed expression.
I must admit that, not being exactly a dilettante, I do not think I would have come up with the provided solution. smile

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: acrobat

Debian Logo