[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: finding the index at which two strings differ
From: |
Bob Proulx |
Subject: |
Re: finding the index at which two strings differ |
Date: |
Tue, 6 May 2008 13:29:13 -0600 |
User-agent: |
Mutt/1.5.13 (2006-08-11) |
Poor Yorick wrote:
> Looking for a simple ways to output the index at which two strings
> differ. Here is one:
>
> cmp <(echo "hello") <(echo "help") | cut -d' ' -f5 | tr -d ,
>
> Any other suggestions?
That seems reasonable to me. Although I tend to use awk and sed for
such things. The concept is basically the same as your suggestion
with one less pipe of character I/O.
awk '{gsub(/,/,"");print$5}'
sed 's/.*byte \([[:digit:]][[:digit:]]*\),.*/\1/'
I can't think of any way to do this natively in bash. I can't think
of a better tool than cmp to do this comparison. But perhaps someone
else more clever than I will have a more elegant solution.
Bob