Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pattern matching

Status
Not open for further replies.

sandeepmur

Programmer
Dec 14, 2003
295
PT
Hi,

I have the fwg code :
Code:
grpName="abc"
name="SBL|ttt|abc"

echo $name | awk -v g=$grpName 'index($1,"|"g)!=0 { print "HELLO"}'

my problem is that I want to print "HELLO" only when grpName is found as it is in name variable. In the above code, if i leave out 'c' in grpName, it still prints HELLO.

any suggestions ?
thnx
 
This is normal behaviour:
"SBL|ttt|abc"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Yes, I understand that this is normal. So, how do I make sure that the string grpName is present entirely in string name ?

thnx
 
I can probably use the split function, break up the name variable into an array, use a for loop to chk whether the var grpName exists in the array.. but this appears quite tedious..

any better alternative ?
 
If by chance you play with gawk you may try this:
echo $name | gawk -v g=$grpName '$1~"(^|\|)"g"(\||$)"{ print "HELLO"}'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I substituted gawk with just awk and it works great.. just one question:

can I put $1~"(^|\|)"g"(\||$) within an IF condition so I can have an else construct ??

I tried inserting an if condition but it gave a :

syntax error The source line is 3.
The error context is
>>> if <<< ( $4~"(^|\|)"g"(\||$)" ) { sub(/-/, "LOCKED", buf) };


thnx
 
The if instruction shouldn't be in the pattern section but the action, ie inside { }.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
thnx.. I placed the if instruction inside {} but continue hv some trouble.. heres the code..

Code:
awk -v g=$grpName '
{buf=$0}
 {if($4~"(^|\|)"g"(\||$)") { then sub(/-/, "LOCKED", buf)  }
	else {
	print "HELLO" > "err.txt"
	}
};
{ print buf}

The problem is that the else construct is always being executed even if the IF condition fails.

thnx
 
oops.. the "then" is outside the {} and the error obtained is :

syntax error The source line is 3.
The error context is
{if($4~"(^|\|)"g"(\||$)") then >>> { <<<
awk: The statement cannot be correctly parsed.
The source line is 3.
syntax error The source line is 4.

thnx
 
In the man awk or info gawk page pay attention to the syntax of the if statement.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I had already tried this:

if($4~"(^|\|)"g"(\||$)") sub(/-/, "LOCKED", buf) [ else print "helllo" > abc.txt ]

but getting the same error.
 
if($4~"(^|\|)"g"(\||$)") sub(/-/, "LOCKED", buf); else print "helllo" > "abc.txt"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
problem continues..

syntax error The source line is 5.
The error context is
>>> if <<< ($4~"(^|\|)"g"(\||$)") sub(/-/, "LOCKED", buf); else print "helllo" > "abc.txt"


thnx
 
I showed you the correct syntax for the if ... else one-line statement.
It should, as previously stated, be inside { }.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi,

When inside the {}, it doesnt give any error but the ELSE construct gets executed again even when the if condition proves true.

 
Can you please post your whole actual program, samples input, expected result and actual result ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top