Bash脚本查找两个字符串之间的差异 [英] Bash script Find difference between two strings
本文介绍了Bash脚本查找两个字符串之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出以下两个字符串:
"12345"
"1245"
第一个是完整的字符串,第二个是第一个缺少的内容,我希望它返回"3".
Where the first one is the complete string and the second has things missing from the first, I want it to return "3".
再次使用:
"The ball is red"
"The is red"
我想归还球"
我尝试了diff与
diff <(echo "12345") <(echo "1245")
但是diff无法提供所需的输出. comm也不执行我想要的操作.
But diff isn't giving the desired output. comm doesn't do what I want either.
推荐答案
我认为comm
是正确的命令:
comm -23 <(tr ' ' $'\n' <<< 'The ball is red') <(tr ' ' $'\n' <<< 'The is red')
或更灵活:
split_spaces() { tr ' ' $'\n' <<< "$1"; }
split_chars() { sed $'s/./&\\\n/g' <<< "$1"; }
comm -23 <(split_spaces 'The ball is red') <(split_spaces 'The is red')
comm -23 <(split_chars 12345) <(split_chars 1245)
这篇关于Bash脚本查找两个字符串之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文