I am looking for a way to highlight the differences between 2 strings. The idea is to show, in a terminal, what characters were changed by iconv. Both strings are already processed to remove leading and trailing spaces, but internal spaces must be handled.
RED="$(tput setaf 1)" ## Short variables for the tput ->
CYA="$(tput setaf 6)" ## -> commands to make output strings ->
CLS="$(tput sgr0)" ## -> easier to read
str1="[String nâmè™]" # String prior to iconv
str2="[String name[tm]]" # String after iconv -f utf-8 -t ascii//translit
Ultimately I want to automate the formatting of the differences so they are surrounded by tput color codes that I can echo to the terminal.
${str1}
= Highlight in red, characters not common to both strings
${str2}
= Highlight in cyan, characters not common to both strings
Wanted Output:
output1="[String n${RED}â${CLS}m${RED}è™${CLS}]"
output2="[String n${CYA}a${CLS}m${CYA}e[tm]${CLS}]"
Most diff utilities I looked at work on the line or word level. I was thinking of parsing the output of cmp for the byte# of the first diff, but I would have to re-parse for multiple differences it seems.
Anyway I think about it, it seems like it going to be an involved process so I just want to make sure I'm not missing an obvious solution or tool.
Right now I'm thinking the easiest way would be to format each string to put a single byte on a new line and then my options open up.
nstr1="$(fold -w1 <<< "$(echo "${str1}")")"
nstr2="$(fold -w1 <<< "$(echo "${str2}")")"
diff <(echo -e "${nstr1}") <(echo -e "${nstr2}")
This is as far as i got and didn't want to go further unless I was on the right track. I'm certain there is a zillion ways to do this but is there a more efficient way to go here?