我有一些腳本產生的顏色,我需要擺脫這一點。刪除輸出顏色
#!/bin/bash
exec > >(tee log) # redirect the output to a file but keep it on stdout
exec 2>&1
./somescript
的輸出(在日誌文件中):
java (pid 12321) is [email protected][60G[@[0;32m OK @[0;39m]
我不知道怎麼把這裏的ESC字符,所以我把@
在它的地方。
我改劇本爲:
#!/bin/bash
exec > >(tee log) # redirect the output to a file but keep it on stdout
exec 2>&1
./somescript | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
但現在它給我(在日誌文件中):
java (pid 12321) is [email protected][60G[ OK ]
我怎麼也刪除此'@[60G
?
也許有一種方法可以完全禁用整個腳本的着色?
BSD/OSX用戶:我們通常沒有-r選項來sed。 'brew install gnu-sed'將會安裝一個支持的版本。用'gsed'運行。 –
如果我做'echo'$(tput setaf 1)foo $(tput sgr0)bar「| sed -r「s/\ x1B \ [([0-9] {1,2}(; [0-9] {1,2})?)?[mGK] // g」| cat -A',我得到:'foo^O bar $'所以我猜一些字符沒有被正確刪除,對吧?你知道如何糾正? – edi9999
@ edi9999據我所知,不同之處在於超過16種顏色的顏色設置(如'setaf'支持)需要的參數不止兩個;我的正則表達式支持兩個。改變第一個'?'出'''應該有所幫助。處理'sgr0'是可能的,但基於搜索,它可能會超出這個基於正則表達式的答案的範圍。 –