How does linux compare the content of a file?
How does linux compare the differences in file content? In view of this problem, today the editor summarizes this article on document comparison, hoping to help more friends who want to solve this problem to find a more simple and feasible way.
The Linux file comparison command is used to compare the difference between the contents of the two files. There are mainly comm command and diff command. If you want to compare two ordered files, you can use the comm command. If you want to compare two text files line by line, you can use the diff command to list the differences.
Use the common command to compare:
The comm command is a very concise command with only two parameters. However, the three parameters are often used in combination, and our common usage is as follows:
Comm-12 displays only lines that exist in both files
Comm-23 displays only lines that appear in the first file but not in the second file
Comm finds lines in file 2 that are not in file 1:
Comm-23 2.txt 1.txt
Note: the contents of the two files must be sorted by sort before comparing them. Otherwise, the output will be wrong.
Diff command comparison:
The diff command is a classic text comparison tool, and the diff command has more parameters than comm. It is often used in combination with the patch command for patch upgrades. The default is the-a parameter, which compares the differences between the two files line by line. To achieve the desired results here, we also need to work with grep and awk utility:
Diff 2.txt 1.txt | grep "