Linux compares the different __linux between two files

Source: Internet
Author: User

Turn from: http://www.361way.com/comm-diff-awk-file/2112.html

Recently in writing a comparison of the/ETC/DHCPD file configuration IP and arp-n get the IP alignment of the script. This is to find out the difference between the two output files-that is, there is no part in file 1 in file 2, or in file 2 that is not part of file 1. To implement this feature, there are four common methods on the Web, but in the actual test it is found that the result of one method is inaccurate. That is, there are actually three common methods that can be implemented.

Method One: Comm Command implementation

Parameters for the Comm command

-1   does not display columns that appear only in the 1th file.
-2   does not display columns that appear only in the 2nd file.
-3   does not display columns that appear only in 1th and 2nd files.

The Comm command is a very concise command with only two parameters. However, three parameters are often used in combination, our common usage is as follows:

COMM-12 shows     only rows that exist in two files;
comm-23    displays only the rows that appear in the first file but not in the second file;
comm-123  doesn't show anything.

Comm find the lines in file 2 that are not in file 1:

Cat/etc/dhcpd.conf|grep "fixed-address" |grep-v ^#|awk ' {print $NF} ' |sed ' s/;//g ' |sort >/tmp/1.txt arp-n|grep
E Ther|grep-v Eth0|awk ' {print $} ' |sort >/tmp/2.txt
comm-23 2.txt 1.txt

Note: The contents of two files must be sorted before they are compared. Otherwise, the output will be wrong.

Method Two: diff command comparison

The diff command is a classic text comparison tool, and the diff command is more than Comm. It is often combined with the patch command for patch upgrades. By default, the-a parameter is used to compare the differences between two files on a line-by-row basis. Here we want to achieve the results we want, and we need to work with grep and awk utility:

Diff 2.txt 1.txt |grep "<" |awk ' $ = '

Note: It is also found that when comparing using the diff command, you need to sort the files in advance, otherwise the output will be incorrect.

Method Three: awk implementation

Awk should be the Taishanno of the shell's usual commands, and the work of almost any other command can be done by awk (though some may be more complex to write). This example is no exception:

awk ' nr==fnr{a[$0]++} nr>fnr&&!a[$0] ' 1.txt 2.txt

Find the same part between two files to use

awk ' nr==fnr{a[$0]++} nr>fnr&&a[$0] '  1.txt 2.txt

The following two statements can also be replaced by:

awk  ' nr==fnr{a[$0]}nr>fnr{if (!) ( In a) print $ ' file1 file2 find different values in file 2
awk  ' nr==fnr{a[$0]}nr>fnr{if (in a)    print $} ' file1 file2 find The same value in the two file

Note:

1. When awk is implemented, you do not need to sort the two files in advance,

2. Note that the placement order of two files in the previous two commands is different. Three implementation methods, the order of the file must be reversed, upside down, the effect just want to reverse, it becomes to find file 1 in the file 2 does not have the line.

Method Four: grep fraught method

Another bad way to spread the web is to implement the grep command:

After testing, regardless of whether I have done a sort reverse in advance on two files, the results of this method output are incorrect. Maybe grep can do the same, but I have a problem with the parameters I'm using. However, if someone can do it directly through grep, please don't hesitate to tell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.