Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top