1819850 Members
2476 Online
109607 Solutions
New Discussion юеВ

Re: awk not outputting

 
SOLVED
Go to solution
Keith_107
Occasional Contributor

awk not outputting

I am trying to compare 2 strings in a script two see if the contents of one string is in the other string. For instance, is "cd" in "abcdef"?

I settled on the awk 'index' command which appears to be able to return the starting location of the string within the other string. Using the example above with S1 = "abcdef" and S2 = "cd", awk '{index($S1,$S2);}'
For my purposes a value > 0 would tell me I have a match. Fine.

I'm new to awk and have been trying to get the syntax correct. Sometimes Tru64 gives me a syntax error so I know I'm running awk. The other times I get nothing; the script just stops and I have to Ctrl-C to end it. The same thing happens when I run the simplest of awk commands (awk '{print "Hello";}' from the command line; no response so I have to Ctrl-C to get the Tru64 prompt back.

I have searched Tru64 doc, google and the man page and found lots of info but nothing on how to get the results of my awk commands to display or end up in a script variable.

What am I doing wrong?

KS
7 REPLIES 7
Keith_107
Occasional Contributor

Re: awk not outputting

Oops! Looks like this is in the HPUX forum.

Can anybody move it to Tru64?

KS
A. Clay Stephenson
Acclaimed Contributor

Re: awk not outputting

No need to move, the answer's the same in HP-UX land. Awk variables are only prefixed with '$' if they are positional variables (e.g $1, $2, ...); other user variables are simply S1, S2 so in your case change $S1 ro S1 and awk will love you.
If it ain't broke, I can fix that.
Keith_107
Occasional Contributor

Re: awk not outputting

This is in my script:
T1=addchgdel
echo $T1
T2=add
echo $T2
awk '{index(T1,T2);}'
if [ $? -ne 0 ] ; then
echo "hit"
else
echo "miss"
fi

When I run the script it responds:
addchgdel
add
Then it just hangs

Any ideas?
Muthukumar_5
Honored Contributor

Re: awk not outputting

Your awk in script is waiting for input there.
You missed to give that,

Try as,

T1=addchgdel
echo $T1
T2=add
echo $T2
echo "$T1 $T2" | awk '{index(T1,T2);}'
if [ $? -ne 0 ] ; then
echo "hit"
else
echo "miss"
fi

It will work there.

HTH.
Easy to suggest when don't know about the problem!
Peter Nikitka
Honored Contributor

Re: awk not outputting

If you use Muthukumars approach, you have to change
..
echo "$T1 $T2" | awk '{index(T1,T2);}'
..
to

echo "$T1 $T2" | awk '{index($1,$1;}'

Another possibility is the match-operator:
T1=addchgdel
echo $T1
T2=add
echo $T2
if echo "$T1 $T2" | awk '$2 !~ $1 {exit 1}'
then echo ok
else echo not matched
fi
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Muthukumar_5
Honored Contributor
Solution

Re: awk not outputting

OOOPs. sorry,

Try as,

T1=addchgdel
echo $T1
T2=add
echo $T2

echo "$T1 $T2" | awk '{
print index($1,$2) }'
if [ $? -ne 0 ] ; then
echo "hit"
else
echo "miss"
fi

HTH.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: awk not outputting

We can use awk's match() to do this too as,

T1=addchgdel
echo $T1
T2=add
echo $T2
echo "$T1 $T2" | awk '{ print match($1,$2) }'
if [ $? -ne 0 ] ; then
echo "hit"
else
echo "miss"
fi

It is too work there.

HTH.
Easy to suggest when don't know about the problem!