1

I am working on VSCode 1.51 (at this point in time the most recent version), and while running terraform plan .. from the integrated PowerShell (5.1 on this workstation) I noticed that if I was redirecting the output to a text file, either via the operator (.. > .\a.txt) or via the Tee-Object (.. | Tee-Object -FilePath .\a.txt) the produced output file was mangled by ASCII escape sequences; specifically by Control Sequence Introducer (CSI) sequences, such as ESC[1m. In the output files I have seen so far I observed only the following 6 sequences:

- ESC[0m
- ESC[1m
- ESC[4m
- ESC[31m
- ESC[32m
- ESC[33m

I am using Notepad++ 7.8.7 to read the files, how do I replace the above sequences with a single space character (' ')?

Update

Obviously, if it could be possible to remove the ASCII escape sequences from the terraform plan output it would be even better.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
aledeniz
  • 431
  • 3
  • 13
  • 1
    To whoever has flagged this question as clone of https://stackoverflow.com/questions/28269278/can-i-programmatically-burn-in-ansi-control-codes-to-a-file-using-unix-utils, those are two completely unrelated questions. – aledeniz Nov 15 '20 at 11:40

2 Answers2

5

As per the documentation of Notepad++ 7.8.7:

Notepad++ regular expressions use the Boost regular expression library v1.70, which is based on PCRE (Perl Compatible Regular Expression) syntax, only departing from it in very minor ways.

I replaced the escape sequences directly in Notepad++, using the following regular expression to find the strings to replace:

\e\[(.*?)m

and replacing them with a single space character (' ' without the quotes inside the Replace with: field on the Notepad++ Replace dialog). In details:

  • \e matches the escape character,
  • \[ matches the opening square bracket,
  • \e\[ matches therefore the Control Sequence Introducer ("ESC ["),
  • (.*?)m matches any string until the first occurrence of the character 'm'.
aledeniz
  • 431
  • 3
  • 13
4

This is not a direct answer to your question as you asked it, but I just wanted to note that terraform plan has a -no-color option which will disable the control codes and just emit plain text at the source, avoiding the need to strip the codes out later.

Martin Atkins
  • 62,420
  • 8
  • 120
  • 138
  • Thank you @martin-atkins, that's a great suggestion, I may leverage that parameter when I need to investigate the terraform plan output on a text file. – aledeniz Nov 15 '20 at 11:42
  • I have marked this as the best answer. I have updated the question, to state that removing the root cause would be a more desirable outcome. – aledeniz Nov 15 '20 at 11:47