Awk – not able to specify FS


I'm processing a manpage in nroff format with awk to extract the options to each command… I figured out that the options start with \fB, followed by the actual option, and maybe \fP and option arguments and so on…



I started writing an awk-script, specifing FS = "\fB" … well, it didn't work… I tried to escape the \, switching to FS = "\\fB" but didn't work either… what am I doing wrong?

This is my script:

    FS = "\\f." # "\\\\f." didn't work either

    print $2

This is the input


Where I want $2 to be -o. But it just won't work.

Best Solution

It looks like you can accomplish this with 4 backslashes:

$ echo "1\z2\z3" | awk 'BEGIN { FS = "\\\\z" } ; {print $3 $1}'

When bash parses this, it should unescape the 4 backslashes to 2 literal backslashes; then awk will unescape those 2 backslashes to a single literal backslash.

Related Question