how to grep a subdomain

When I try to grep a subdomain having three dots("."), I am getting another fully qualified domain also that looks same as the sub domain but with hyphen("-") in the place of dot(".")

I just need the first result. Please help me...

grep abcd.efg.xyz.info /etc/named.conf

zone "abcd.efg.xyz.info" {
        file "/var/named/abcd.efg.xyz.info.db";
zone "abcd-efg-xyz.info" {
        file "/var/named/abcd-efg-xyz.info.db";
grep 'abcd\.efg\.xyz\.info' /etc/named.conf

try the subdomain in quotes..

grep "abcd\.efg\.xyz\.info" /etc/named.conf

is there any other way? b'cos I am writing a script to search duplicate zone entries in the named.conf file. The script first makes a list of domains in the zone file and then searches for duplicate entries.

So which part isn't working? You haven't explained why the solutions provided wouldn't work.

I am looking for a solution without editing the domain names. i.e. with out adding "\" in the middle.

because......

The problem is that the '.' is a wild-card in regular expressions and will be interpreted as any possible character. In your case you wanted the dot '.' specifically in which it needed to be escaped.

is it possible by using "sed" ?

This is the way regular expressions work. If you would explain why this is a problem you might get better results.

Ok.. I am almost near to my result. I decided to replace the whole dots in the file with \. and then search for the duplicates. Could you please tell me how to replace each . with \. in a file ?

I think you're misunderstanding. You don't need to modify the input file with the escaped dots (\.), just search for domains using the search term with escapes.

My pseudo code:

grep zone input_file | awk '{print $2}' | uniq (or pretty close to that)
for domain in "domain list from grep output"
do
  sed to replace . with \. in $domain
  grep -c escaped_domain from input file to get count
  if count > 1
  then
    echo $domain is not unique, fix file
  fi
done

Thank you for your help peterro. I've finished my script :slight_smile:

You can avoid regular expressions by using fgrep instead of grep.
Try this:

fgrep 'abcd.efg.xyz.info' /etc/named.conf

This willmake your script simpler.

WOW.. thats Cool.... Thank you edidataguy

That is cool, I've not used fgrep in the past. Looking at the man pages, it can also be used as a switch to grep (grep -F) like egrep and grep -E.

You are welcome.
Thanks for getting back.
Most, dont find time to do so once the problem is solved. :slight_smile: