简体   繁体   中英

escaping backslash and bracket on windows awk

I have an awk command that I want to use on cmd . The following command works well in bash , but fails on windows cmd :

echo errr | awk '/err/ { $0 = "\033[32m" $0 "\033[39m" }; 1'

I get the following error on windows:

awk: cmd. line:1: '/err/
awk: cmd. line:1: ^ invalid char ''' in expression

After going through some questions, I changed my command to:

 echo errr | awk "/err/ { $0 = "\033[32m" $0 "\033[39m" }; 1"

but that gives me:

awk: cmd. line:1: /err/ { $0 = \033[32m $0 \033[39m }; 1
awk: cmd. line:1:              ^ backslash not last character on line
awk: cmd. line:1: /err/ { $0 = \033[32m $0 \033[39m }; 1
awk: cmd. line:1:              ^ syntax error

How can I port my command to work in cmd ?

Standard advice when running awk on Windows:

a) don't do it, install cygwin and run awk from there instead

b) if "a" is not possible then create a file "foo.awk", store your script /err/ { $0 = "\\033[32m" $0 "\\033[39m" }; 1 /err/ { $0 = "\\033[32m" $0 "\\033[39m" }; 1 in that, and then run it as awk -f foo.awk to avoid Windows nightmarish quoting rules.

instead of awk 'your commands' use awk -e 'your commands', there should be no error. I do not have windows to check. Will it be coloring the text? Read my comment below your question.

EDIT: OK, now if you have version 6 in PowerShell, it should work coloring like this:

echo errr | awk -e "/err/ { $0 = '`e[32m' $0 '`e[39m'}; 1 "

If you have a different version windows, look for the correct escape sequence in the link I provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM