我有两个包含信息行的字符串。我想获得两个字符串中不同的行。 例: 字符串1:
"This is line1
This is line2
This is line3"
String2的:
"This is line1
This is linex
This is line2"
预期结果:
diff string1 string2 is:
"This is line3"
diff string2 string1 is:
"This is linex"
答案 0 :(得分:7)
您可以使用comm
:
$ str1="This is line1
> This is line2
> This is line3"
$ str2="This is line1
> This is linex
> This is line2"
$ comm -23 <(echo "$str1" | sort) <(echo "$str2" | sort)
This is line3
$ comm -23 <(echo "$str2" | sort) <(echo "$str1" | sort)
This is linex
答案 1 :(得分:1)
你可以用diff
$ str1="This is line1\nThis is line2\nThis is line3"; str2="This is line1\nThis is linex\nThis is line2";
$
$ diff -y -W 30 --suppress-common-lines <(echo -e $str1) <(echo -e $str2)
> This is linex
This is line3 <
受到这个问题和答案的启发:Bash string difference