Linux uses grep to delete the same part of two files

Linux uses grep to delete the same part of two files

First describe this problem: For example, two files file1 and file2, delete the common parts of the two files, and leave the unique parts of the two files

1. The content of the two files is known

[root@grep ~]# cat 1.txt 
aaa
111
bbb
222
ccc
333
ddd
444
eee
555

[root@grep ~]# cat 2.txt 
eee
111
kkk
999
eee
aaa
222
666

2. Use grep

[root@grep ~]# grep -vf 1.txt 2.txt && grep -vf 2.txt 1.txt
kkk
999
666
bbb
ccc
333
ddd
444
555

Two parameters are used here:

  • The parameter -v means invert match, that is, reverse matching, and the output has no matching items.
  • The parameter -f means to read the matching template (pattern) from the file.

The previous part matches the template in the file file1 to reverse match the content in the file file2, that is, the content in the output file file2 that is not in file1. The latter part can be obtained in the same way, the content in the output file file1 that is not in the file2.

For other methods, please refer to here

Guess you like

Origin blog.csdn.net/weixin_43279138/article/details/129096247