Hi, I have searched this long and hard and don't seem to see another post on this issue.
I have two strings each with the same characters but in a different order.
String1=�word�
String2=�dwor�
I want to test them to show their similarity. Unfortunately I can't do a sort so that they will be identical (dorw) as that only works on file. Can someone propose a non sed/awk way of doing this.
sorry I meant identical. This is for the purposes of a test. The strings are already very similar. They only differ in the way of content by order. I want to run a command(s) on them so that they look the same.
but they can look the same in many different permutations, 96 I think for a four letter word. How do you know which permutation you want them to be identical too?
Can you a give a permutation for one that can be put in a test command?
I was hoping to manipulate them so they both are identical.
E.g. sort characters in order
string1=dorw
string2=dorw
They are identical and can be put in a test command, returning true. The strings already have the same length and letters so I thought there must be some way to get them like that.
I'm not sure what you want to do. Do you want to proof the equality of the strings? eg. same length, same chars, same order? Or do you want to put one string of which you already have proven length and same chars in the same order as the first string? Or don't care about the order of either of them, as long as they're in the same order, whatever that order is?
In GNU awk , if you have the words in different files:
awk 'NR==FNR{for(i=1;i<=NF;i++)a1[$i]=$i}
{for(j=1;j<=NF;j++){a2[$j]=$j;if (!($j in a1)){f=1}}}
END {for ( o in a1 )if (!( o in a2 )){f=1}
if (f || ( i != j )) {print "different"}else{print "similar"}
}' FS='' f1 f2