awk split domain name

Hi,

I have a domain name that I need to convert to an LDAP base for this example - "my.domain.name"

I know that I could easily see there are 3 fields and split with awk with

awk -F. '{printf "dc=%s,dc=%s,dc=%s", $1, $2, $3}'

However, what if I have 5 fields or 4 fields, is there away to use NF and for/do loop to create this base line for AD connectivity?

Thank you in advance for any assistance.

Hi, try:

awk -F. '{for (i=1; i<NF; i++) printf "dc=%s,",$i; printf "dc=%s\n",$NF}' 

or perhaps sed:

sed 's/\./,dc=/g; s/^/dc=/'

Or in bash or ksh93 if it is in variable ( var=my.domain.name ) try:

echo "dc=${var//./,dc=}"

Awesome work - love it.
I used the first example.

Thank you very much.