Operating System - HP-UX
1834646 Members
2135 Online
110069 Solutions
New Discussion

Re: how to retrive the server name only from servername.x.y.com

 
Hanry Zhou
Super Advisor

how to retrive the server name only from servername.x.y.com

I have the variable A=servername.x.y.com
but I just want to retrive "servername" portion from the variable, what is the command?
Thanks,
none
3 REPLIES 3
Patrick Wallek
Honored Contributor

Re: how to retrive the server name only from servername.x.y.com

Well you could use awk

NAME=$(echo ${A} | awk -F . '{print $1}')

or you could use cut:

NAME=$(echo ${A} | cut -d . -f 1)
Rick Garland
Honored Contributor

Re: how to retrive the server name only from servername.x.y.com

echo $A | awk -F. '{print $1}'

This will return 'servername'
John Palmer
Honored Contributor

Re: how to retrive the server name only from servername.x.y.com

You could also use the shell's inbuilt parameter substitution which is more efficient as it doesn't have to fork a new process...

If $A contains "servername.x.y.com" then the command:

B=${A%%.*}

gives you "servername" in $B. This works by removing the first "." and everything after it. See man sh-posix for details.

Regards,
John