Home > Archive > AWK > May 2005 > match command ?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| 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|"
| |
| Ed Morton 2005-05-03, 8:55 pm |
|
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.
| |
| Bob Harris 2005-05-03, 8:55 pm |
| 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
|
|
|
|
|