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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using grep returns partial matches, I need to get an exact match or nothing
# 1  
Old 01-24-2011
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.

Code:
$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.

Last edited by Yogesh Sawant; 02-01-2011 at 03:43 AM.. Reason: added code tags
# 2  
Old 01-25-2011
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:

Code:
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):

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

Hope this helps
This User Gave Thanks to agama For This Post:
# 3  
Old 01-25-2011
alternative solution using awk and/or nawk to match the exact zoneName:
Code:
# 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:
Code:
# zoneName=myzone2
# nawk -v zonename=${zoneName} '$2 == zonename {print $2}' /var/tmp/zoneadm.out.test
myzone2


Last edited by ec01; 01-25-2011 at 11:01 AM.. Reason: fix code tags
This User Gave Thanks to ec01 For This Post:
# 4  
Old 01-25-2011
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep for an exact match in a file

I am currently having some issues while trying to grep for a exact string inside a file. I have tried doing this from command line and things work fine i.e. when no match is found, return code=1 but when its done as part of my script it returns 0 for the same command - I dont know if there is an... (6 Replies)
Discussion started by: Ads89
6 Replies

2. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Grep -w not printing exact matches

Dear All, Here is my input TAACGCACTTGCGGCCCCGGGATAAAAAAAAAAAAAAAAAAAAATGGATT NAGAGGGACGGCCGGGGGCATAAAAAAAAAAAAAAAAAAAAAGGGATTTC NGGGTTTTAAGCAGGAGGTGTCAAAAAAAAAAAAAAAAAAAAAGGGATTT NTGGAACCTGGCGCTAGACCAAAAAAAAAAAAAAAAAAAATGGATTTTTG ATACTTACCTGGCAGGGGAGATACCATGATCAATAAAAAAAAAAAAAAAA... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

Grep exact match

Hello! I have 2 files named tacs.tmp and tacDB.txt tacs.tmp looks like this 0 10235647 102700 106800 107200 1105700 tacDB.txt looks like this 100100,Mitsubishi,G410,Handheld,,0,0,0 100200,Siemens,A53,Handheld,,0,0,0 100300,Sony Ericsson,TBD (AAB-1880030-BV),Handheld,,0,0,0... (2 Replies)
Discussion started by: Cludgie
2 Replies

5. Shell Programming and Scripting

Searching for exact match using grep

I am searching for an exact match on a value read from another file to lookup an email address in another file. The file being checked is called "contacts" and it has Act #, email address, and contact person. 1693;abc1693@yahoo.comt;Tommy D 6423;abc6423@yahoo.comt;Jim Doran... (2 Replies)
Discussion started by: ziggy6
2 Replies

6. Shell Programming and Scripting

Grep two words with exact match

HI Input : Counters Counter Int Ints Counters Counters Ints Ints I want to grep Counter|Int Output : Counter (1 Reply)
Discussion started by: pareshkp
1 Replies

7. UNIX for Dummies Questions & Answers

grep and sed exact match questions

This was mistaken as homework in a different forum, but is not. These are questions that are close to what I am trying to do at work. QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1... (1 Reply)
Discussion started by: thibodc
1 Replies

8. Solaris

grep exact match

Hi This time I'm trying to grep for an exact match e.g cat.dog.horse.cow.bird.pig horse.dog.pig pig.cat.horse.dog horse dog dog pig.dog pig.dog.bird how do I grep for dog only so that a wc -l would result 2 in above case. Thanks in advance ---------- Post updated at 06:33 AM... (4 Replies)
Discussion started by: rob171171
4 Replies

9. Shell Programming and Scripting

find/grep returns no matches

Hi all! I've faced with very unintelligible error using find/grep like this: root@v29221:~# find /var/www/igor/data/www/lestnitsa.ru | grep u28507I get nothing as a result, but: root@v29221:~# grep u28507 /var/www/igor/data/www/lestnitsa.ru/_var.inc $db_name = 'u28507';... (2 Replies)
Discussion started by: ulrith
2 Replies

10. UNIX for Advanced & Expert Users

Exact Match thru grep ?????

hey..... i do have text where the contents are like as follows, FILE_TYPE_NUM_01=FILE_TYPE=01|FILE_DESC=Periodic|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=B FILE_TYPE_NUM_02=FILE_TYPE=02|FILE_DESC=NCTO|FILE_SCHDL_TYPE=Daily|FILE_SCHDL=|FILE_SCHDL_TIME=9:00am|RESULTS=M... (2 Replies)
Discussion started by: manas_ranjan
2 Replies
Login or Register to Ask a Question