0
GREEN='\033[0;32m' NC='\033[0m' # No Color rimip2=$(echo -e "${GREEN}222b:b33c:3401:2341:88c2:85d7:5cf6:6a01${NC}" | grep "222b:b33c:3401:2341:88c2:85d7:5cf6:6a01") if [[ "${rimip2}" == "222b:b33c:3401:2341:88c2:85d7:5cf6:6a01" ]]; then echo "#### Same" else echo "#### Different" fi 

This code always print "#### Different" although the visible displayed value of echo ${rimip2} is 222b:b33c:3401:2341:88c2:85d7:5cf6:6a01

If I remove ${GREEN} and ${NC}, then "#### Same" is displayed.

This is a simplified portion of a complex code. So my aim is to maintain printing in GREEN color, and then grepping that line which I will use for ssh. But now I cannot use it for SSH until it is equal to the string value "222b:b33c:3401:2341:88c2:85d7:5cf6:6a01"

How can remove the Green encoding part after grepping the line?

2
  • Try with some string substitution to rip the color codes off. Commented Oct 21, 2019 at 15:09
  • Duplicate of 17998978. Commented Oct 21, 2019 at 15:19

2 Answers 2

1

You can use grep -o to print only the part that matched the expression, so that the presence of any colour codes no longer makes a difference:

rimip2=$(echo -e "${GREEN}222b:b33c:3401:2341:88c2:85d7:5cf6:6a01${NC}" | grep -o "222b:b33c:3401:2341:88c2:85d7:5cf6:6a01") 

You can also just use the exit status from grep directly, at least in this simplified case:

 if echo -e "${GREEN}222b:b33c:3401:2341:88c2:85d7:5cf6:6a01${NC}" | grep "222b:b33c:3401:2341:88c2:85d7:5cf6:6a01" >/dev/null; then echo "#### Same" else echo "#### Different" fi 
Sign up to request clarification or add additional context in comments.

2 Comments

grep has a silent mode if you just want the status -q
In this example, -q would be fine. But if you also want to handle errors, you can't use it, because it exits with status code 0 in case of error too. See: stackoverflow.com/questions/4749330/…
0

This Worked out

GREEN='\033[0;32m' NC='\033[0m' # No Color rimip2=$(echo -e "${GREEN}240b:c01c:221:2341:88c2:85d7:5cf6:6a01${NC}" | grep "240b:c01c:221:2341:88c2:85d7:5cf6:6a01" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g") if [[ "${rimip2}" == "240b:c01c:221:2341:88c2:85d7:5cf6:6a01" ]]; then echo "#### Same" else echo "#### Different" fi 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.