Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

match command ?
Can somebody point me in the right direction.

I am trying to create an awk statement to see if the value of VAR (22)
is in the variable VARLIST. In my example below it is there but by
found flag is not printing 1, which I think it should be.


What do I need to do to get this to work. A working example would be
very much appreciated.

Thanks in advance to anybody who answers this post

#!/bin/ksh


VAR=22
VARLIST="1 22 45"


let found=0
eval `echo ${VAR} | nawk ' (index ($0, pattern))print "found=1"} '
pattern="$VARLIST" `

echo "|$found|"


Report this thread to moderator Post Follow-up to this message
Old Post
Stu
05-04-05 01:55 AM


Re: match command ?

Stu wrote:
> Can somebody point me in the right direction.
>
> I am trying to create an awk statement to see if the value of VAR (22)
> is in the variable VARLIST. In my example below it is there but by
> found flag is not printing 1, which I think it should be.
<snip>
>
> #!/bin/ksh
>
>
> VAR=22
> VARLIST="1 22 45"
>
>
> let found=0
> eval `echo ${VAR} | nawk ' (index ($0, pattern))print "found=1"} '
> pattern="$VARLIST" `
>
> echo "|$found|"
>

You've got your logic backward. This:

index ($0, pattern)

will find the first occurence of pattern (i.e. "1 22 45") in $0 (i.e.
"22"). You want to do the reverse:

index (pattern, $0)

There's better ways to do that in awk but there's absolutely no need for
awk to do that in ksh (hint - use a "case"). If you want more awk help,
follow up here. FOr a ksh solution post to comp.unix.shell.

Ed.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
05-04-05 01:55 AM


Re: match command ?
In article <1115153244.304041.224840@f14g2000cwb.googlegroups.com>,
"Stu" <beefstu350@hotmail.com> wrote:

> Can somebody point me in the right direction.
>
> I am trying to create an awk statement to see if the value of VAR (22)
> is in the variable VARLIST. In my example below it is there but by
> found flag is not printing 1, which I think it should be.
>
>
> What do I need to do to get this to work. A working example would be
> very much appreciated.
>
> Thanks in advance to anybody who answers this post
>
> #!/bin/ksh
>
>
> VAR=22
> VARLIST="1 22 45"
>
>
> let found=0
> eval `echo ${VAR} | nawk ' (index ($0, pattern))print "found=1"} '
> pattern="$VARLIST" `
>
> echo "|$found|"

if [[ $VARLIST =    $VAR\ * || \
$VARLIST = *\ $VAR\ * || \
$VARLIST = *\ $VAR ]]; then
echo "found"
fi

Or make sure VARLIST always has a token before and after each value

VARLIST=":1:22:45:"
if [[ "$VARLIST" = *:$VAR:* ]]; then
echo "found"
fi

OK, now beat me up for not giving an awk answer :-)

Bob Harris

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Harris
05-04-05 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:50 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.