问题
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