bash最长的两个字符串的共同部分

时间:2014-04-25 14:08:18

标签: bash

我有以下字符串:“abcdefx”,“zzdefghij” 我想提取两个字符串的共同部分,即这里“def”。 我尝试使用sed但我不能这样做,除非公共部分是这样的前缀:

fprint "%s\n%\n" | sed -e 'N;s:\(.*\).*\n\1.*:\1:'

2 个答案:

答案 0 :(得分:4)

我认为这听起来很有趣,这是我的解决方案:

first="abcdefx"
second="zzdefghij"

for i in $(seq ${#first} -1 1); do
    for j in $(seq 0 $((${#first}-i))); do
        grep -q "${first:$j:$i}" <<< "$second" && match="${first:$j:$i}" && break 2
    done
done

echo "Longest common substring: ${match:-None found}"

输出:

Longest common substring: def

答案 1 :(得分:4)

这个纯bash脚本将以相当有效的方式找到其两个参数中第一个最长的子字符串:

#!/bin/bash

if ((${#1}>${#2})); then
   long=$1 short=$2
else
   long=$2 short=$1
fi

lshort=${#short}
score=0
for ((i=0;i<lshort-score;++i)); do
   for ((l=score+1;l<=lshort-i;++l)); do
      sub=${short:i:l}
      [[ $long != *$sub* ]] && break
      subfound=$sub score=$l
   done
done

if ((score)); then
   echo "$subfound"
fi

演示(我称之为脚本banana):

$ ./banana abcdefx zzdefghij
def
$ ./banana "I have the following strings: abcdefx, zzdefghij I would like to extract the common part of the two strings, i.e. here def." "I tried with sed but I couldn't do that unless the common part was a prefix like this"
 the common part