The question of FS of AWK

Hi guys,
there is sth of the FS makes me confused, as the following described:
here is a example of the file:

cat file
dir1/file1.txt
dir1/file2.txt
dir1/file3.txt
dir1/file4.txt

what i want to get is :

awk -v FS="/|\\." '{print $2, NF}' file

as u see, it works well when just type the code in the terminal.

but, if i try to do that in the script file:

#!/bin/sh
awk -v FS="/|\\\." '{print $2, NF}' file

i have to use three "\\\" to escape the ".", why?

could anyone explain the details of REGEXP of the FS defined of AWK? thanks in advanced !

PS: OS:
Linux px 2.6.22.18-0.2-default #1 SMP 2008-06-09 13:53:20 +0200 i686 i686 i386 GNU/Linux
the version of awk:
GNU Awk 3.1.5g

It's because you aren't protecting the entire line from the shell. You should be moving the single quotes (') to encompase the entire awk expression, not just the {print} part.
Each layer of interpretation removes one level of escape, so direct commandline, strips one \, the awk command strips the next, leaving you looking for a . character.
In the shell script, the first \ is stripped, leaving \\. This is passed to sh, which strips another \, leaving \. Awk then strips the last one off.

The entire awk script is quoted, but you are right that it would be better to move the assignment of FS into the awk script.

It should be placed in a BEGIN block:

awk 'BEGIN { FS = "/|\\." }
{print $2, NF}' "$FILE"

:smiley: Good point :smiley:

Personally I like brackets

-v FS='[/|.]'
..or just
-F'[/|.]'

excellent explanation ! thanks a lot , guys.
ps:
to danmero: r u Chinese? guess by :slight_smile:

My Guess would be Montreal Canada. 45.48 -73.6

TIMTOWTDI...

awk -F"/|[.]" '{print $2, NF}' file

No, I'm not. I'm from Transylvania.

Correct, that's my location.