Compare two files in linux, and compare two files in linux
Recently, I am writing a script to compare the configuration IP address of the/etc/dhcpd file with the IP address obtained by arp-n. This requires finding out the difference between the two output files-that is, there is no part in file 1 but in file 2, or there is a part in file 2 but not in file 1. There are four common methods on the Internet to implement this function. However, in actual tests, it is found that the results of one method are inaccurate. There are actually three common methods to achieve this.
Method 1: comm command implementation
Comm Command Parameters
-1 does not show columns that appear only in 1st files. -2 do not show columns that appear only in 2nd files. -3: columns that appear only in files 1st and 2nd are not displayed.
The comm command is a very concise command with only two parameters. However, three parameters are usually used in combination. Our common usage is as follows:
Comm-12 only displays the rows that exist in both files; comm-23 only displays the rows that appear in the first file but not in the second file; comm-123 then nothing is displayed.
Comm finds the rows in file 2 and not in file 1:
cat /etc/dhcpd.conf|grep "fixed-address"|grep -v ^#|awk '{print $NF}'|sed 's/;//g'|sort > /tmp/1.txtarp -n|grep ether|grep -v eth0|awk '{ print $1}'|sort >/tmp/2.txtcomm -23 2.txt 1.txt
Note: before comparing the content of the two files, you must sort the content in sort. Otherwise, the output result is incorrect.
Method 2: Comparison of diff commands
The diff command is a classic text comparison tool. The diff Command requires more parameters than comm. It is often used in combination with the patch command for patch upgrade. By default, the-a parameter is used to compare the differences between two files one by one. Here we want to implement the desired results. We also need to use grep and awk in combination:
diff 2.txt 1.txt |grep "<"|awk ' $1 = " " '
Note: We also found that when using the diff command for comparison, we also need to sort the compared files by sort in advance. Otherwise, the output results are also incorrect.
Method 3: awk implementation
Awk should be regarded as a common shell command in Taishan Beidou. Almost all the work that can be done by many other commands can be done by awk (but some may be complicated to write ). In this example:
awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' 1.txt 2.txt
You can use
awk 'NR==FNR{a[$0]++} NR>FNR&&a[$0]' 1.txt 2.txt
The following two statements can also be replaced:
Awk 'nr = FNR {a [$0]} NR> FNR {if (! ($1 in )) print $0} 'file1 file2 find different values in file 2 awk 'nr = FNR {a [$0]} NR> FNR {if ($1 in) print $0} 'file1 file2 find the same value in the two files
Note:
1. When awk is implemented, two files do not need to be sorted by sort in advance,
2. Note that the placement order of the two files is different from that of the preceding two commands. In the three implementation methods, the order of the files must be reversed, and the results will be reversed, and it will become the rows in file 1 that are not found in file 2.
Method 4: grep
Another method for spreading an error on the internet is implemented using the grep command:
grep -v -f 1.txt 2.txt
After testing, no matter whether or not I performed the sort Reverse Order on the two files in advance, the results output by this method are all incorrect. Maybe grep can also meet this requirement, but there is a problem with the parameters I use. However, if someone can directly implement it through grep, please do not recommend