pi-2r
pi-2r

Reputation: 1279

bash script extract domain name for LDAP

I am currently working on a script that asks the user entered a domain name. This domain name is used to fill the hqbase variable for LDAP. For example, if the user enter "example.com", i have to cut the domaine name with example on the side and "com" another side.

I found this example:

echo http://example.com/index.php | awk -F/ '{print $3}'

But how to split example from com (any ".").

Upvotes: 4

Views: 1300

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85865

You want . as your delimiter:

# Using cut
$ echo "domain.com" | cut -d. -f1
domain

$ echo "domain.com" | cut -d. -f2
com

# Using awk                                                                     
$ echo "domain.com" | awk -F. '{print $1}'
domain

$ echo "domain.com" | awk -F. '{print $2}'
com

# Save values                                                                   
$ first=$(echo "domain.com" | cut -d. -f1)
$ second=$(echo "domain.com" | cut -d. -f1)

Upvotes: 3

Related Questions