0

I have a sentence which is nothing but a string variable with words separated by spaces .

I have another variable which contains a word . What I wanted is to find exact matching of the word in the variable containing the sentence . I have tried this :

read -p "enter the word to match " STR # STR contains VVDNSG 

#this DUP_SG_NAME_PUBLIC variable contains a string which consists of few words 
DUP_SG_NAME_PUBLIC=$(aws ec2 describe-security-groups \
  --query 'SecurityGroups[*].{GroupName:GroupName}' \
  --output text) 

if [[ $DUP_SG_NAME_PUBLIC =~ $STR ]]
then 
echo "true"
else 
echo "false"
fi

Problem Here I have provided "VVDN" as string but I have to match "VVDNSG" but this code works for both . Even for VVDN and VVDNSG which is understandable .

What I want to do
I want to find exact match that is if given VVDNSG as STR value it should print true or else false even with VVDN and I want that to do the comparison with variables not with literal strings .

I have tried few examples googling most of them either tell you to find match in a file or hardcode the string comparison.

1
  • Seems like a case with an exact match first and a wildcard match second might be what you want. Commented Aug 4, 2018 at 14:36

1 Answer 1

3

Your STR works as a regex. So, to use it as an exact match, explicitly mark the beginning with ^and the end with $:

STR="^VVDN$"

Then the if condition will be true only for exact matches of STR in DUP_SG_NAME_PUBLIC.

Update: Actually, I think what you want is somewhat different - you have a sentence containing words separated by spaces (you didn't mention a termination character) and you want to match one of the words, only if it is an exact match. Let's assume it will be an exact match if it has a space after the word or if it is at the end of the sentence.

In that case, your regex could be:

STR="VVDN($| )"

Is this what you meant?

Sign up to request clarification or add additional context in comments.

3 Comments

Yes you are right , I have a sentence containing words stored in variable and I want to match one of the words only if it is an exact match . Let me check your solution .Problem is I take input from the user to be stored in STR variable . I know that ^ and $ mark the begin and end but only if I could use them in user input . I have edited the question.
I am a little sketchy on awk and sed , I don't know if these tools could work here .
After storing the input from the user you can always append something to it. For example, you can do STR="${STR}($| )". If you want the exact match to be extended to the whole word, which means the match could happen at the beginning of the sentence or when there's a space before the match, you could change it to STR="(^| )${STR}($| )".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.