bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: at which byte do two strings differ


From: Stephane CHAZELAS
Subject: Re: at which byte do two strings differ
Date: Tue, 6 May 2008 13:08:42 +0200 (CEST)
User-agent: slrn/pre0.9.9-102 (Linux)

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]