Code Comments
Programming Forum and web based access to our favorite programming groups.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|"
Post Follow-up to this message
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.
Post Follow-up to this messageIn 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
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.