1

I am trying to match two strings (IpAddress) as below. But it's not matching.

i=192.168.2.29
ipCheckInConfig="SG_1=192.168.2.24,192.168.2.29
> SG_2=192.168.2.20,192.168.2.23,192.168.2.31"

if echo "$i" | egrep -q "$ipCheckInConfig" ; then
    echo "Matched"
else
    echo "Not Matched"      
fi

Could someone please help?

2
  • 2
    The pattern is passed as a parameter, and the input is passed via standard input; you have them reversed. Commented Sep 18, 2013 at 15:01
  • 1
    What Tim said...and the pattern you're looking for is bigger than the input string you're giving to egrep, so of course it can't find the big string in the little one. You need to echo the long string and search for the short one. Or use the built-in regex operator if your shell is bash — the Unix and Shell tags don't make that a slam-dunk. Commented Sep 18, 2013 at 15:08

1 Answer 1

4

You don't need to call egrep for that. Use bash's internal regex capabilities:

if [[ "$ipCheckInConfig" =~ $i ]]; then
    echo "Matched"
else
    echo "Not Matched"      
fi
Sign up to request clarification or add additional context in comments.

4 Comments

Strictly, the question isn't tagged 'bash' and not all shells have the bash regex function.
Oh sorry for overlooking that fact. user2531569: Are you using bash by any chance?
Thanks for ur reply. Yes i am using Bash only.
If you're using BASH then my suggested answer should work fine.

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.