问题
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?
回答1:
To put it all together:
#!/usr/bin/env bash
# Using stdin input, outputs each char. on its own line, with actual newlines
# in the input represented as literal '\n'.
toSingleCharLines() {
sed 's/\(.\)/\1\'$'\n''/g; s/\n$/\'$'\n''\\n/'
}
# Using stdin input, reassembles a string split into 1-character-per-line output
# by toSingleCharLines().
fromSingleCharLines() {
awk '$0=="\\n" { printf "\n"; next} { printf "%s", $0 }'
}
# Prints a colored string read from stdin by interpreting embedded color references
# such as '${RED}'.
printColored() {
local str=$(</dev/stdin)
local RED="$(tput setaf 1)" CYA="$(tput setaf 6)" RST="$(tput sgr0)"
str=${str//'${RED}'/${RED}}
str=${str//'${CYA}'/${CYA}}
str=${str//'${RST}'/${RST}}
printf '%s\n' "$str"
}
# The non-ASCII input string.
strOrg='[String nâmè™]'
# Create its ASCII-chars.-only transliteration.
strTransLit=$(iconv -f utf-8 -t ascii//translit <<<"$strOrg")
# Print the ORIGINAL string with the characters that NEED transliteration
# highlighted in RED.
diff --changed-group-format='${RED}%=${RST}' \
<(toSingleCharLines <<<"$strOrg") <(toSingleCharLines <<<"$strTransLit") |
fromSingleCharLines | printColored
# Print the TRANSLITERATED string with the characters that RESULT FROM
# transliteration highlighted in CYAN.
diff --changed-group-format='${CYA}%=${RST}' \
<(toSingleCharLines <<<"$strTransLit") <(toSingleCharLines <<<"$strOrg") |
fromSingleCharLines | printColored
This yields:
回答2:
Answer provided by @Thomas Dickey in comments that there was no tool or process that was vastly easier than the way I was attempting it.
Just to finish up, I was able to produce the "Wanted Output" simple enough with the following diff
lines.
diff --changed-group-format="\${RED}%=\${CLS}" <(echo -e "${nstr1}") <(echo -e "${nstr2}")|tr -d '\n'
diff --changed-group-format="\${CYA}%>\${CLS}" <(echo -e "${nstr1}") <(echo -e "${nstr2}")|tr -d '\n'
Unfortunately I haven't figured out how to echo the output to interpret the color codes, but that's another question.
来源:https://stackoverflow.com/questions/34376884/highlight-string-differences