Search code examples
awkcharacter

Difficulties with awk sub command output when there is a + sign in the last field


folks:

I am having a problem with the awk sub command when passing it a string that contains the + symbol in the last field. I am trying to output all but the last field in the record without the trailing field separator. For example:

echo "dog cat fish hamster" | awk 'sub(FS $NF, x)'

This produces the desired output of "dog cat fish"

echo "dog cat fish +hamster" | awk 'sub(FS $NF, x)'

This produces what appears to be no output.

echo "dog +cat fish hamster" | awk 'sub(FS $NF, x)'

This also produces the expected output of "dog +cat fish"

I am using awk to parse a configuration file with one or more source directories and a single destination directory for use in transfering files with scp. Some of the destination directories have a +, such as /app/tgv+/in and I am unable to change the mount points because of legacy code. The problem only seems to occur when the + appears in the last field. I would be very grateful if you could help me determine if I have done something wrong or if this is simply a problem with the awk string parser. Thanks so much for any assistance.

DGC


Solution

  • The problem here is that the first argument of sub() is supposed to be a regular expression. This doesn't bother in most cases of matching absolute strings. But in your case FS $NF will become +hamster which will be treated as a regular expression and thus won't get matched with anything and thus, anything won't get printed.

    Even if you use something like {1}hamster instead of +hamster, there won't be any output.

    A better approach will be to change the way. A simple method can be

    echo "dog cat fish +hamster" | awk 'NF--'