bash中两个字符串的最长公用前缀
发布时间:2020-12-15 19:26:51 所属栏目:安全 来源:网络整理
导读:我有两个字符串。为了这个例子,它们是这样设置的: string1="test toast"string2="test test" 我想要的是找到从字符串开头开始的重叠。有了重叠,我的意思是我上面的例子中的字符串“test t”。 # So I look for the command command "$string1" "$string2"
我有两个字符串。为了这个例子,它们是这样设置的:
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 "%sn%sn" "$string1" "$string2" | sed -e 'N;s/^(.*).*n1.*$/1/' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |