2008-05-06, 01:53(-04), Nathan Coulter:
> Looking for a simple ways to output the byte at which two strings differ.
> Here
> is one:
>
> cmp <(echo "hello") <(echo "help") | cut -d' ' -f5 | tr -d ,
>
> Any other suggestions?
I'd suggest you fix the locale to being C for more portable
results.
LC_ALL=C cmp <(echo "hello") <(echo "help") |
cut -d' ' -f5 | tr -d ,
(note that <(...) is not standard, only in some ksh, zsh and
bash)
You could implement cmp for strings with awk, something like:
LC_ALL=C awk '
BEGIN {
s1 = ARGV[1]; s2 = ARGV[2]
do {
a = substr(s1, ++i, 1); b=substr(s2, i, 1);
if (a != b) {print i; exit 1}
} while (a != "" && b != "")
exit 0
}' hello help
--
Stéphane