Awk – not able to specify FS

awk

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…

Example:

\fB\-\-author\fR

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:

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

{
    print $2
}

This is the input

\fB-o\fP

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}'
31

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