Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove ^[, and all of the escape sequences in a file using linux shell scripting

We want to remove ^[, and all of the escape sequences.

sed is not working and is giving us this error:

$ sed 's/^[//g' oldfile > newfile; mv newfile oldfile; sed: -e expression #1, char 7: unterminated `s' command  $ sed -i '' -e 's/^[//g' somefile sed: -e expression #1, char 7: unterminated `s' command 
like image 551
hasan Avatar asked Jun 30 '11 12:06

hasan


People also ask

How do you escape and in shell script?

Escaping is a method of quoting single characters. The escape (\) preceding a character tells the shell to interpret that character literally. With certain commands and utilities, such as echo and sed, escaping a character may have the opposite effect - it can toggle on a special meaning for that character.

How do I delete ANSI escape sequences?

You can use regexes to remove the ANSI escape sequences from a string in Python. Simply substitute the escape sequences with an empty string using re. sub(). The regex you can use for removing ANSI escape sequences is: '(\x9B|\x1B\[)[0-?]


2 Answers

Are you looking for ansifilter?


Two things you can do: enter the literal escape (in bash:)

Using keyboard entry:

sed 's/Ctrl-vEsc//g'

alternatively

sed 's/Ctrl-vCtrl-[//g'

Or you can use character escapes:

sed 's/\x1b//g' 

or for all control characters:

sed 's/[\x01-\x1F\x7F]//g' # NOTE: zaps TAB character too! 
like image 184
sehe Avatar answered Sep 28 '22 08:09

sehe


commandlinefu gives the correct answer which strips ANSI colours as well as movement commands:

sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" 
like image 27
Tom Hale Avatar answered Sep 28 '22 08:09

Tom Hale