Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching for a string in an array question.

Status
Not open for further replies.

wraheem

MIS
Jul 19, 2001
62
US
Hi,

Using awk, I have taken the output of a command and entered it into an array. Afterwards, I want to test to see if each item in the array matches a certain 1st few letters of a string like below:

for (item in array)
if (array[item] == "issv")
{
print array[item]
}
There are multiple items in the array that would match the "issv" string like below:
issv400
issv406
issv301

I have tried to say if (array[item] == "issv*"), but it wont match the 3 above. The only way it will work is if I say if (array[item] == "issv400").

I know there has to be a way to match all the items that match the "issv" part.

Any help would be appreceiated.

 
Try

if (array[item] ~ /^issv/)


CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Try using the expr expression which will evaluate as a like statement would in SQL

for (item in array)
if (expr "$var" : "issv*")

{
print array[item]
}




In this case $var would be your array[item]

Hope this helps,


Steve
 
Steve,

Unless I'm missing something, this is an AWK forum - not 'UNIX scripting' [wink].

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I used the CaKiwi method and it worked to perfection, thanks to you all!!

 
vgersh99,


I needed more coffee. Too many regex expressions and not enough sleep will make you forget where you are.

;-)


Steve
 
Steve was trying to help. A better solution could lie elsewhere, and the person asking the question does not always have chosen the best forum.
I would entertain all suggestions, and in the 'worst' case, a new thread could be started to benefit the participants of the other forum.
By the way, who doesn't need coffee when we are addicted to all these interesting subjects?
Best regards to you all, and ALL your posts are much appreciated.
 
The issue was mixing shell syntax/tools within awk solution/domain - if you read Steve's post closer.

I've never been known to be a 'purist', but I guess there's time for everything! [wink]

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks Vlad, I guess I missed the mixed syntax part. Sorry about that and hope you don't mind.
I think we're all on the same wavelength.
Have a good time posting and reading.
Best regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top