3

I have an output file from a testing script (which I cannot alter), the output looks great in the terminal thanks to the encoding, which displays the output in nice colours.

However when I vim the file, I get the following:

^[[1m0024^[[0m, ^[[36munknown.10^[[0m --> ^[[32mUNKNOWN^[[0m

I would rather the file contained:

0024, unknown.10 --> UNKNOWN

There are a couple of similar questions on stackover flow, but so far I have not found a solution that works for me.

Any help would be greatly appreciated!

Many thanks!

Additional info:

I don't want to conceal the colour characters, I would like to remove them from the file.

The output goes into an evidence file, and then that file is pushed up to a GIT for the team to review. It is difficult to the GIT UI with those colour codes :(

jeff_h
  • 519
  • 1
  • 9
  • 26
  • See http://unix.stackexchange.com/questions/7695/how-to-make-vim-display-colors-as-indicated-by-color-codes – Māris Kiseļovs Apr 11 '16 at 11:41
  • i don't want to conceal the colour characters, I would like to remove them. The output goes into an evidence file, and then that file is pushed up to a GIT for the team to review. It is difficult to the GIT UI with those colour codes :( – jeff_h Apr 11 '16 at 13:20

2 Answers2

4

To remove color control character, you may use the following sed command:

sed 's/\x1b\[[^\x1b]*m//g' file

As indicated in here, the a color code is composed of <Esc>[FormatCodem.

The escape character is \x1b in hexadecimal (sometimes noted as \e or \033).

The command looks for the sequence escape followed by square bracket \x1b\[ until the character m, if found it deletes it.

Everything in between these 2 characters is allowed except the escape character itself [^\x1b]*. This allows to have the shortest regex.

oliv
  • 12,690
  • 25
  • 45
  • 1
    on further analysis, it looks like your regex removes some data by accident :( 0011, e --> z Should be: 0011, zone_name.src_zone_name --> dmz – jeff_h Apr 11 '16 at 16:39
  • 1
    It's difficult to guess what is wrong without the exact sample that gives problem. You also may try `sed 's/\x1b\[[^m]*m//g'`. If this doesn't work, please copy/paste the input data. – oliv Apr 12 '16 at 05:43
  • Thank you for the answer oliv! Your answer removes m characters if there are one or more m characters after the coloring, but this doesn't remove m characters: `sed 's/\x1b\[[0-9]*m//g'` – propatience Apr 06 '21 at 12:26
1

If you can't remove them from the tool producing the output, you could still remove them afterwards with the following sed command :

sed -r 's/\^\[\[[0-9]{1,2}m//g'

Example :

$ echo """^[[1m0024^[[0m, ^[[36munknown.10^[[0m --> ^[[32mUNKNOWN^[[0m""" |  sed -r 's/\^\[\[[0-9]{1,2}m//g'
0024, unknown.10 --> UNKNOWN
Aaron
  • 24,009
  • 2
  • 33
  • 57
  • I ran sed like this: cat bad_file | sed -r 's/\^\[\[[0-9]{1,2}m//g' >> good_file and it still had the unwanted text. I assume I am running the command incorrectly? – jeff_h Apr 11 '16 at 13:33
  • 1) yes, you should run sed like this : `sed -r 's/\^[[[0-9]{1,2}m//g' badFile` if you want the output on stdout (i.e. to redirect it : `sed pattern badFile > goodFile`) or `sed -ir 's/\^[[[0-9]{1,2}m//g' badFile` if you want to edit the file 'in-place'. 2) I think you will have to use oliv's answer instead, I did not take special characters into account – Aaron Apr 11 '16 at 13:37
  • Actually your command is not wrong in itself and should work. It's just easier to pass the file to the sed command rather than using `cat`, and the `-i` flag makes file edition easier. – Aaron Apr 11 '16 at 13:39