bash中两个字符串之间的差异

geo*_*a_e 5 bash diff

我有两个包含信息行的字符串.我想获得两个字符串中不同的行.示例:String1:

"This is line1
This is line2
This is line3"
Run Code Online (Sandbox Code Playgroud)

String2的:

"This is line1
This is linex
This is line2"
Run Code Online (Sandbox Code Playgroud)

结果预期:

diff string1 string2 is:
"This is line3"

diff string2 string1 is:
"This is linex"
Run Code Online (Sandbox Code Playgroud)

dev*_*ull 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
Run Code Online (Sandbox Code Playgroud)