Using grep returns partial matches, I need to get an exact match or nothing

I�m trying to modify someone perl script to fix a bug. The piece of code checks that the zone name you want to add is unique. However, when the code runs, it finds a partial match using grep, and decides it already exists, so the �create� command exits.

$cstatus   = `${ZADM} list -vic | grep ${zoneName}`;

At the shell, it�s this: zoneadm list -vic | grep ${zoneName}
I have an old zone name called cscndmndc006-old. I wanted to create a new zone name called cscndmndc006.

I tried grep �w but that still got a partial match.

zoneadm list -vic | grep -w ${zoneName}

  • cscndmndc006-old installed /export/czones/cscndmndc006-old native shared

I�m running Solaris 10 update 9 on SPARC. Grep �x isn�t an available option.

The -w option on grep isn't going to work as it appears that the zone name contains 'non-word' characters (a dash). Since this seems a pretty specific application in terms of your input (it seems that the data you are searching for is always in column 2), then something as simple as this might work for you:

grep " $zonename " filename

This will match the name you are checking with something in the file only if the string in the file has a leading and trailing space.

If the columns are separated by either a space or tab, then something like this would work (assuming egrep is available):

egrep "[ \t]$zonename[ \t]" filename

Hope this helps

1 Like

alternative solution using awk and/or nawk to match the exact zoneName:

# cat /var/tmp/zoneadm.out.test
  ID NAME             STATUS     PATH                           BRAND    IP
   0 global           running    /                              native   shared
   1 myzone1          running    /zones/myzone1                 native   shared
   2 myzone1-old      running    /zones/myzone1-old             native   shared
   4 myzone2          running    /zones/myzone2                 native   shared
   5 myzone2-old      running    /zones/myzone2-old             native   shared
   6 myzone2-another  running    /zones/myzone2-another         native   shared
   7 myzone2-junk     running    /zones/myzone2-junk            native   shared
# zoneName=myzone2
# awk '$2 == "'$zoneName'"' /var/tmp/zoneadm.out.test
   4 myzone2          running    /zones/myzone2                 native   shared
# zoneName=myzone2-
# awk '$2 == "'$zoneName'"' /var/tmp/zoneadm.out.test  ### no match
# zoneName=myzone2-old
# awk '$2 == "'$zoneName'"' /var/tmp/zoneadm.out.test
   5 myzone2-old      running    /zones/myzone2-old             native   shared

(in your case you'd run zoneadm list -vic then pipe it to awk instead rather than using the file)

Be careful with the quotes for the shell variables (ie: quote as exactly as shown). nawk may be easier if the quotes are too hard to read:

# zoneName=myzone2
# nawk -v zonename=${zoneName} '$2 == zonename {print $2}' /var/tmp/zoneadm.out.test
myzone2
1 Like

Thanks for the replies. ec01, I like your solution as I prefer not to have to save the result to a file. I tested it and it works as advertised. Thanks again.