简体   繁体   English

在 AWK 中转义反斜杠

[英]Escaping backslash in AWK

I'm trying to understand why the command below doesn't work (output is empty):我试图理解为什么下面的命令不起作用(输出为空):

echo 'aaa\tbbb' | awk -F '\\t' '{print $2}'

I would expect the output to be 'bbb'.我希望输出为'bbb'。

Interestingly this works (output is 'bbb'):有趣的是,这是有效的(输出是 'bbb'):

echo 'aaa\tbbb' | awk -F 't' '{print $2}'

And this works as well (ouptut is 'tbbb'):这也有效(输出是 'tbbb'):

echo 'aaa\tbbb' | awk -F '\\' '{print $2}'

It looks as if \\\\\\t is read as backslash followed by tab instead of escaped backslash followed by t .看起来好像\\\\\\t被读作反斜杠后跟 tab而不是转义的反斜杠后跟 t

Is there a proper way to write this command?有没有写这个命令的正确方法?

You need to tell echo to interpret backslash escapes.您需要告诉echo解释反斜杠转义。 Try:尝试:

$ echo -e 'aaa\tbbb' | awk -F '\t' '{print $2}'
bbb

man echo would tell: man echo会告诉:

   -e     enable interpretation of backslash escapes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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