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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

using string match in nawk

Reply to Message Icon

Name: tomput
Date: August 12, 2005 at 12:12:03 Pacific
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



Sponsored Link
Ads by Google

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

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


0

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

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


0

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

You're welcome

Luke Chi


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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


Sponsored links

Ads by Google


Results for: using string match in nawk

using string variable in awk www.computing.net/answers/unix/using-string-variable-in-awk/8040.html

Using a Period in Awk as \. - Bug? www.computing.net/answers/unix/using-a-period-in-awk-as-bug/7808.html

Possibility to use Intel NIC in Sun Ultra5 www.computing.net/answers/unix/possibility-to-use-intel-nic-in-sun-ultra5/1634.html