我想检查(技巧或任何快捷方式)文本字符串是否存在于两个文件中。
第一个文件的内容是
a.txt
b.txt
c.txt
d.txt文件二的内容是
c.txt
a.txt
d.txt如何检查文件1中的字符串是否在文件2中有匹配的字符串?
发布于 2020-03-18 18:36:04
与join,sort和grep
join <(sort /path/to/source) <(sort /path/to/destination) | grep '<string to check>测试
cat source
a.txt
b.txt
c.txt
d.txt
cat destination
c.txt
a.txt
d.txt
join <(sort source) <(sort destination) | grep 'a.txt'
a.txt
join <(sort source) <(sort destination) | grep 'b.txt'如果要求检查两个文件的内容是否不匹配,则可以发出以下命令
cmp --silent <(sort source) <(sort destination) || echo "files are different"测试
cmp --silent <(sort source) <(sort destination) || echo "files are different"
files are different添加源文件中不包含在/var/tmp/不匹配文件中的所有行
comm -23 <(sort source) <(sort destination) > /var/tmp/unmatched从源文件中删除目标文件中未包含的所有行
comm -1 <(sort source) <(sort destination) >| source因为我们在bash,如果您已经将noclobber设置为set -o noclobber,那么您应该使用语法>|。
https://unix.stackexchange.com/questions/573633
复制相似问题