3

The following command

grep -w '^[^#]* machine_VIP' /etc/hosts | head -1 | awk '{print $1}'

works fine on linux (its capture the machine_VIP alias and return the IP address from /etc/hosts and ignore remarks lines)

Example

cat /etc/hosts
5.5.5.5  GH_T  T1 T2 T3 # machine_VIP
# 198.2.3.12 MON1 MON2  machine_VIP
18.2.4.12 W1 # machine_VIP # machine_VIP # machine_VIP
192.9.200.77 machine_linux1 machine_linux2  machine_VIP
192.9.200.78 machine_linux10 machine_linux20 machine_VIP_test
grep -w '^[^#]* machine_VIP' /etc/hosts | head -1 | awk '{print $1}'
192.9.200.77

but on Solaris I have the same hosts and when I run the command I not get anything on the output.

So how to fit the command to Solaris?

1
  • What is the purpose of head -1? Without head -1 in your example the result is the same.
    – Cyrus
    Nov 9, 2014 at 16:00

2 Answers 2

2

On Solaris grep, the -w option means that the entire pattern must match only one word, which is different from the GNU grep meaning of -w that the match must form whole words.

If you add a $ to the end of your pattern then that will work with the example file above, and then you won't need the -w switch:

grep '^[^#]* machine_VIP$' /etc/hosts | head -1 | awk '{print $1}'

... or you could of course install GNU grep on your solaris machine if that's practicable.

1
  • Note that machine_VIP$ will not get a match if "machine_VIP" is not located at the end of the line (which is possible).
    – aff
    Nov 18, 2014 at 3:15
1

With Solaris 11 use the already installed GNU grep: /usr/gnu/bin/grep

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .