I want to determine whether a specific pattern is present within a line or not
e.g.
The whole line is in a varaible called VALUE
VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)"
i want to set a flag to 1 if i find the presence of ABC in the above variable.
Please tell me how to do it in a Shell Script?
Thanks in advance
VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)"
mFlag=`echo $VALUE | egrep -c 'ABC'`
echo "mFlag = "$mFlag
vino
June 27, 2007, 9:08am
3
skyineyes:
I want to determine whether a specific pattern is present within a line or not
e.g.
The whole line is in a varaible called VALUE
VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)"
i want to set a flag to 1 if i find the presence of ABC in the above variable.
Please tell me how to do it in a Shell Script?
Thanks in advance
if [[ "$VALUE" = *ABC* ]] ; then
flag=1
fi;
Thanks alot
Just a modification to this.
If i want to test the presence of two patterns in one go then how this line should look like. Suppose i want to check the presence of both "ABC" and "PAR" and then set the value if EITHER ONE OF THEM OR BOTH EXIST
i.e LOGICAL OR CONDITION FOR PRESENCE OF BOTH.
VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)"
mFlag=`echo $VALUE | egrep -c 'ABC|PAR'`
echo "mFlag = "$mFlag
Thanks alot man.
I am really thankful
shell_life:
VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)"
mFlag=`echo $VALUE | egrep -c 'ABC|PAR'`
echo "mFlag = "$mFlag
aigles
June 27, 2007, 9:31am
7
If you test the presence of 'GQS' the egrep command will set the flag to 1, because the 'GQS' is found (inside 'AGQSAs').
If you want to test for the entire value, you must modify the egrep RE:
VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)"
mFlag=`echo $VALUE | egrep -c '[(, ](XXX|GQS)([),]|$)'`
echo "mFlag = "$mFlag
The result will be 0 since there is no element equal to GQS nor XXX inside VALUE
You are right Aigles.
The solution I gave is specific for the example.
Thanks aigles
But my requirement is to match only whole word , Not a subpattern and thus is think that following is sufficient
VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)"
mFlag=`echo $VALUE | egrep -c 'ABC|PAR'`
echo "mFlag = "$mFlag
aigles:
If you test the presence of 'GQS' the egrep command will set the flag to 1, because the 'GQS' is found (inside 'AGQSAs').
If you want to test for the entire value, you must modify the egrep RE:
VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)"
mFlag=`echo $VALUE | egrep -c '[(, ](XXX|GQS)([),]|$)'`
echo "mFlag = "$mFlag
The result will be 0 since there is no element equal to GQS nor XXX inside VALUE