Escaping backslash in AWK

久未见 提交于 2020-08-10 18:48:44

问题


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

Interestingly this works (output is 'bbb'):

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

And this works as well (ouptut is '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.

Is there a proper way to write this command?


回答1:


You need to tell echo to interpret backslash escapes. Try:

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

man echo would tell:

   -e     enable interpretation of backslash escapes


来源:https://stackoverflow.com/questions/17971197/escaping-backslash-in-awk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!