AWK: pattern not properly stored in variable?

Hey there,

I have a table of contents file of the form

1 Title1
1.1 Subtitle1
1.1.1 Subsubtitle1
1.1.2 Subsubtitle2
...

and want to count the number of dots in the first field to find out the level of the section.
I use the gsub function for the job, which works if I pass the pattern directly.
The awk program

/.*/ {
count = gsub(/\./, "&", $1)
print count
}

correctly prints out the number of dots, output of the first four lines (see sample file above) is

0
1
1
2

If I try to store the dot-pattern in a variable things mess up. The program

/.*/ {
separator = /\./
count = gsub(separator, "&", $1)
print count
}

outputs

0
2
3
2

Can anyone explain to me what is happening and supply a solution on how to store the substitution pattern in a variable?

Thanks in advance,

Simon

---------- Post updated at 11:31 AM ---------- Previous update was at 11:13 AM ----------

Solved, sorry for giving up that early and asking you. As variables only store string values, the pattern has to be saved as

separator = "\."

and can then be used, e.g. as

count = gsub(separator, "&", $1)

Damn, that was easy :slight_smile:

That's interesting because "\." doesn't work on my awk!

I have to use "\\."

Hi Scott,

I initially also typed "\\." but it doesn't seem necessary with my interpreter (mawk on Debian Lenny).

Regards,

Simon