Search code examples
regexterraformnotepad++ansi-escape

How to strip the ANSI escape codes from a text file?


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.


Solution

  • 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.