Computing.Net > Forums > Unix > using string match in nawk

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

using string match in nawk

Reply to Message Icon

Original Message
Name: tomput
Date: August 12, 2005 at 12:12:03 Pacific
Subject: using string match in nawk
OS: SunOS 5.8
CPU/Ram: sun
Comment:

I want to match for a string that contains "/"
I'm using nawk from within a bourne shell script.

the nawk I'm using is:
nawk '$0 ~ /'$class_name*.class'$/ {print $0}'

$class_name could contain a string like:
javax/activation/MimeType

stdin to nawk contains strings with forward slashes.

how do I construct a match in nawk where the string to match, and std, both contain "/"?

here's my error message from nawk:
nawk: syntax error at source line 1
context is
$0 ~ >>> /javax/activation/MimeType*. <<< class$/ {print $0, jar_name}
nawk: bailing out at source line 1

I'm assuming nawk doesn't like the forward slashes.

Thanks in advance for your help and suggestions.
Tom


Report Offensive Message For Removal


Response Number 1
Name: Luke Chi
Date: August 12, 2005 at 13:55:47 Pacific
Reply: (edit)

There're too many things to talk about. Study it yourself carefully.

#!/bin/ksh

class_name="javax/activation/MimeType[0-9a-zA-Z_]*.class"

echo "javax/activation/MimeTypeTEST.class" | nawk -v class_name_in="$class_name" '$0 ~ class_name_in {print $0}'

Output:
javax/activation/MimeTypeTEST.class

Note: [:print:] and [:alnum:] don't work well on my Sun machine. So, I used [0-9a-zA-Z_]* instead.


Luke Chi


Report Offensive Follow Up For Removal

Response Number 2
Name: tomput
Date: August 12, 2005 at 14:32:19 Pacific
Reply: (edit)

wow, Luke thanks so much, that worked great!

since for my script the class_name is a commandline option, it's easy to append the pattern "[0-9a-zA-Z_]*.class" to it.

Thanks again for your help.
Tom


Report Offensive Follow Up For Removal

Response Number 3
Name: Luke Chi
Date: August 12, 2005 at 15:49:14 Pacific
Reply: (edit)

You're welcome

Luke Chi


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home








Do you have a Desktop Computer anymore?

No
Yes, but only at work
Yes, but its rarely used
Yes, and its a workhorse


View Results

Poll Finishes Today.
Discuss in The Lounge
Poll History




Data Recovery Software