I use the command find . -maxdepth 1 -not -type d
which generates output like ./filename.1.out
I pipe the find command output to awk. The goal is to split on either the literal ./ or .. I have it working using:
find . -maxdepth 1 -not -type d | gawk 'BEGIN { FS = "(\\./)|(\\.)" } ; { print NF }'
In fact it works if I drop the first backslash in the first set of paren. Ex:
find . -maxdepth 1 -not -type d | gawk 'BEGIN { FS = "(\./)|(\\.)" } ; { print NF }'
What I don't understand - and my question is why does it not work if I use:
find . -maxdepth 1 -not -type d | gawk 'BEGIN { FS = "(\./)|(\.)" } ; { print NF }'
By "not work" I mean NF returns with a number as if the second paren was a regex . character (to match any type of character). Maybe I'm answering my own question... but as I look at the commands/behavior it would appear that the initial backslash is being ignored. In fact, there was a warning escape sequence message saying that \. was being treated as plain '.'. But I didn't really understand what it was doing until I began printing NF.
And indeed... the awk doc for escape sequences (https://www.gnu.org/software/gawk/manual/html_node/Escape-Sequences.html#Escape-Sequences) say:
The backslash character itself is another character that cannot be included normally; you must write
\\to put one backslash in the string or regexp.
So if I wanted to wring a regex to match a dollar sign then I would need FS="\\$"?
The post was originally to ask why it was happening. Then I believe I may have pieced things together. If I am wrong then please set me straight.
(\./|\\.)means field delim is 'either any character and slash, or dot (by itself)'. It happens that in your input the only character that ever precedes slash is dot. Similarly(\./|\.)does indeed match any character (and every character) as a field delimiter. FYI you don't need the parentheses. For FS as a regex to match$yes you must escape. Note however that if FS is a single character it is NOT treated as a regex, just a character, so the single character$will also work.-not -type drather than-type f?-not -type ddoes not mean-type f. Like not negative does not mean positive, it's can be zero.