bash中两个字符串的最长公用前缀

我有两个字符串。为了这个例子,它们是这样设置的:
string1="test toast"
string2="test test"

我想要的是找到从字符串开头开始的重叠。有了重叠,我的意思是我上面的例子中的字符串“test t”。

# So I look for the command 
command "$string1" "$string2"
# that outputs:
"test t"

如果字符串是string1 =“atest toast”; string2 =“test test”,它们将不会重叠,因为检查从开头开始,而在字符串1的开始处为“a”。

在sed中,假设字符串不包含任何换行符:
string1="test toast"
string2="test test"
printf "%s\n%s\n" "$string1" "$string2" | sed -e 'N;s/^\(.*\).*\n\1.*$/\1/'

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...