Skip to main content
edited body
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265
ram="rambo"

ram1="rimbo"

awk -v ram=$ram -v ram1=$ram1 '{ for (i=1;i<=length(ram);i++) { if (substr(ram,i,1) != substr(ram1,i,1)) { count++ } }} END { print (count/length(ram)*100"% difference") }' <<< ""

output:

20% difference

The above example assumed that bithboth variables ram and ram1 are always the same size in length. We pass both variables to awk and check each character one by one against the one in the other string, tracking the differences with a count variable.

At the end, we work out the percentage of the string that is different.

ram="rambo"

ram1="rimbo"

awk -v ram=$ram -v ram1=$ram1 '{ for (i=1;i<=length(ram);i++) { if (substr(ram,i,1) != substr(ram1,i,1)) { count++ } }} END { print (count/length(ram)*100"% difference") }' <<< ""

output:

20% difference

The above example assumed that bith variables ram and ram1 are always the same size in length. We pass both variables to awk and check each character one by one against the one in the other string, tracking the differences with a count variable.

At the end, we work out the percentage of the string that is different.

ram="rambo"

ram1="rimbo"

awk -v ram=$ram -v ram1=$ram1 '{ for (i=1;i<=length(ram);i++) { if (substr(ram,i,1) != substr(ram1,i,1)) { count++ } }} END { print (count/length(ram)*100"% difference") }' <<< ""

output:

20% difference

The above example assumed that both variables ram and ram1 are always the same size in length. We pass both variables to awk and check each character one by one against the one in the other string, tracking the differences with a count variable.

At the end, we work out the percentage of the string that is different.

added 8 characters in body
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265

ram="rambo"

ram1="rimbo"

ram="rambo"

ram1="rimbo"

awk -v ram=$ram -v ram1=$ram1 '{ for (i=1;i<=length(ram);i++) { if (substr(ram,i,1) != substr(ram1,i,1)) { count++ } }} END { print (count/length(ram)*100"% difference") }' <<< ""

output:

20% difference

The above example assumed that bith variables ram and ram1 are always the same size in length. We pass both variables to awk and check each character one by one against the one in the other string, tracking the differences with a count variable.

At the end, we work out the percentage of the string that is different.

ram="rambo"

ram1="rimbo"

awk -v ram=$ram -v ram1=$ram1 '{ for (i=1;i<=length(ram);i++) { if (substr(ram,i,1) != substr(ram1,i,1)) { count++ } }} END { print (count/length(ram)*100"% difference") }' <<< ""

output:

20% difference

The above example assumed that bith variables ram and ram1 are always the same size in length. We pass both variables to awk and check each character one by one against the one in the other string, tracking the differences with a count variable.

At the end, we work out the percentage of the string that is different.

ram="rambo"

ram1="rimbo"

awk -v ram=$ram -v ram1=$ram1 '{ for (i=1;i<=length(ram);i++) { if (substr(ram,i,1) != substr(ram1,i,1)) { count++ } }} END { print (count/length(ram)*100"% difference") }' <<< ""

output:

20% difference

The above example assumed that bith variables ram and ram1 are always the same size in length. We pass both variables to awk and check each character one by one against the one in the other string, tracking the differences with a count variable.

At the end, we work out the percentage of the string that is different.

Source Link

ram="rambo"

ram1="rimbo"

awk -v ram=$ram -v ram1=$ram1 '{ for (i=1;i<=length(ram);i++) { if (substr(ram,i,1) != substr(ram1,i,1)) { count++ } }} END { print (count/length(ram)*100"% difference") }' <<< ""

output:

20% difference

The above example assumed that bith variables ram and ram1 are always the same size in length. We pass both variables to awk and check each character one by one against the one in the other string, tracking the differences with a count variable.

At the end, we work out the percentage of the string that is different.