加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 21:59:29 所属栏目:安全 来源:网络整理
导读:我有两个包含信息行的字符串.我想获得两个字符串中不同的行. 例: String1中: "This is line1This is line2This is line3" String2的: "This is line1This is linexThis is line2" 结果预期: diff string1 string2 is:"This is line3"diff string2 string
我有两个包含信息行的字符串.我想获得两个字符串中不同的行.
例:
String1中:

"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"

解决方法

你可以使用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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读