0

I have some smart script, that check name of server and get domain name. For example, i have name of server: example.ru01. I need to get: example.ru My scipt:

#!/bin/bash

hostname=example.com01
echo $hostname
reg0="\(\(\w*\.[a-z]*\)\|\(\w*\.[a-z]*\.[a-z]*\)\)"
domain=`expr match $hostname $reg0`
echo $domain

It is ok. in output i have:

example.com01
example.com

But, in my infrastructure, i have some domains with hyphens. For example: test-test.com01. But it doesn't working in my script. How to resolve this problem ? Please help. I made some changes in my regular expression, like this:

\(\(\w*\.[a-z_-]*\)\|\(\w*\.[a-z_-]*\.[a-z_-]*\)\)

But it doesn't work. Where i have error ? Please help. Thanks for your attention.

2 Answers2

0

If the numbers only occur at the end of your hostnames, you also could just remove all numbers from the end. Which I think is easier to read and maintain.

hostname=example.com01
echo $hostname
domain=$(echo $hostname | sed -e 's/[0-9]*$//g')
echo domain
Thomas
  • 4,225
  • 5
  • 23
  • 28
  • that's very nice. but in this case, i need to use regular expressions. for example, i have hosts, like that: test-test.comNY01 – Piduna Valeriu Dec 03 '16 at 19:21
  • In that case you would have to extend the regular expression to match `[A-Z]` as well. Not sure how much diversity your hostnames provide, I thought it would be easier to just cut off the last part. – Thomas Dec 03 '16 at 19:24
  • 1
    Or just use hostname=${hostname%[0-9][0-9]} – Chris Rees Dec 04 '16 at 07:32
0

The problem with your regular expression is that you tell it that it must first match your string with zero to infinity amounts of \w which "Matches any word character including underscore", followed by a literal dot .. (\w*\.)

in the case of test-test.com01 it does not match because of the hyphen, so if you change it to match also - then it will work the way you want it to:

\(\([a-z_-]*\.[a-z_-]*\)\|\([a-z_]*\.[a-z_-]*\.[a-z_-]*\)\)
      ^ replace \w            ^ replace \w

There are several ways to improve this regular expression but IMO the amount of time you should put into making a good one is proportional to the complexity of the text you parse.

Petter H
  • 3,443
  • 1
  • 16
  • 19