Linux compares two files differently (6/21)

Source: Internet
Author: User
Tags diff

CMP command: Compare any two types of files, and the results output to the standard output, the default file is the same output, different file output differences

Necessary parameters

-C Display different information
-l list all the different information
-S error message not prompted
Select parameters

-i< characters > specified number of characters
-V Display version information
--HELP Display Help information

"Return value of function"

0: The same as file
1: The files are different
>1: Error occurred

Comm will compare the two files that have been sorted by line. The display results include 3 columns:

The 1th column is the row found only in File1, the 2nd column is the row found only in File2, and the 3rd column is the common line for two files.

Parameters

Parameters file1 and file2 are the path names of the files to be compared by Comm . When a hyphen (-) is used to replace file1 or file2 ,comm will read the input information from the standard input

Options

-1 does not display the 1th column (that is, rows found only in File1 are not displayed)

-2 does not display the 2nd column (that is, rows found only in File2 are not displayed)

-3 does not display the 3rd column (that is, rows found in two files are not displayed)

Attention

If the file is not sorted, Comm will not work properly.

The 2nd column has no row preceded by a tab, and the 3rd column has two tabs in front of each row

The diff command is a very important tool on Linux to compare the contents of a file, especially to compare two different versions of files to find the changes. Diff prints each line's changes on the command line. The latest version of diff also supports binary files. The output of the diff program is called Patch, because there is also a patch program in the Linux system that updates the contents of the A.C file to B.C based on the diff output. Diff is an integral part of version control tools like SVN, CVS, and Git.

The diff command can compare individual files or directory contents. If you specify that the comparison is a file, it is only valid if the input is a text file. Compare the similarities and differences of text files in a row-by-line manner. If you specify a table of contents, the diff command compares a text file with the same name in two directories. Lists the different binaries, common subdirectories, and files that appear only in one directory.

3. Command parameters:

-Specify how many lines of text to display. This parameter must be used in conjunction with the-C or-u parameter.

The-A or--text diff preset will only compare text files line by row.

-B or--ignore-space-change do not check for differences in whitespace characters.

-B or--ignore-blank-lines does not check for blank lines.

-C Displays all the text and marks the difference.

-C or--context is the same as executing the "-c-" directive.

-D or--minimal use different algorithms to compare in smaller units.

-D or ifdef the output format of this parameter is available for the predecessor Processor macro.

-E or--ed the output format of this parameter is available for the ed script file.

The-f or-forward-ed output is formatted like an ED script file, but is displayed differently in the order of the original files.

-h or--speed-large-files can speed up when comparing large files.

-L or--ignore-matching-lines if two files are different in a few lines, and these lines both contain the characters or strings specified in the option, the differences between the two files are not displayed.

-I or--ignore-case do not check for differences in case.

-L or--paginate the results to the PR program for paging.

-N or--rcs displays the comparison results in RCS format.

-N or--new-file when comparing directories, if file a only appears in a directory, the preset will display: Only in directory: file A If you use the-n parameter, diff will compare file A with a blank file.

-P If the file is compared to a C language code file, the name of the function where the difference is displayed.

-P or--unidirectional-new-file is similar to-N, but this file is compared to a blank file only if the second directory contains a file that is not in the first directory.

-Q or--brief display only if there is no difference, no detailed information is displayed.

-R or--recursive to compare files in subdirectories.

-S or--report-identical-files if no differences are found, the message is still displayed.

-S or--starting-file when comparing directories, start comparing from the specified file.

-T or--expand-tabs expands the tab character when it is output.

-T or--initial-tab the tab character before each line to align.

-u,-u or--unified= displays the different contents of the file in a merged manner.

-V or--version displays version information.

-W or--ignore-all-space ignores all whitespace characters.

-W or--width specifies the column width when using the-y parameter.

-X or--exclude does not compare the files or directories specified in the options.

-X or--exclude-from you can save the file or directory type as a text file, and then specify the text file in =.

-Y or--side-by-side displays the similarities and differences of the files in a parallel manner.

--help display Help.

--left-column when using the-y parameter, if the contents of one row of two files are the same, the contents of that row are displayed only in the field on the left.

--suppress-common-lines displays only the differences when using the-y parameter.

The differences after comparison are listed in the specified form, as follows:

N1 a n3,n4  n1,n2 D n3  

Where the letters "a", "D", "C" are respectively added, deleted and modified operations. While "N1", "N2" represents the line number in file 1, "N3", "N4" represents the line number in file 2.

Note: The instructions above specify the line numbers in each of the two files and their corresponding actions. In the output form, each line is followed by several rows that are affected. Where the line starting with < belongs to file 1, the line starting with > is in File 2.

11,12d10″ indicates that the first file has the 11th and 12 lines more than the second file.

Diff log2013. Log log2014. Log -y -W

"|" Indicates that the contents of the front and back 2 files are different

“< "indicates that the following file is 1 lines less than the previous file“> "indicates that the following file has 1 more lines than the previous file

The meaning of the 2,4c2,4 is that the first [2,4] line in a file (note that this is a closed interval, including lines 2nd and 4th) needs to be modified to match the [2,4] row in the second file.
The next part tells us what needs to be modified, the section of the front with < represents the contents of the [2,4] line of the left file, and the > section represents the contents of the [2,4] line of the right file, and the middle---is the separator symbol for two file contents.

Diff log2014. Log log2013. Log -u

-- log2014. Log---£ º 54.000000000 + 0800 ++ + log2013. Log ---£ º£26.000000000 + 0800

"-" indicates the document before the change, "+ + +" means the document after the change.

Diff log2013. Log log2014. Log -c

2345 The "+" Comparison of the files of the latter is more than the previous line "-" compared to the latter of the file's less line "! "Compare files with two different lines

Parameters of the Comm command

1    does not show  columns that appear only in the first 1 files. 2 does not show  columns that appear only in the first 2 files. 3 does not show    columns that appear only in paragraphs 1 and 2. 

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:

-      displays only the rows that exist in the two files;-displays only the rows that appear in the first file that do not appear in the second file;-123 does not show anything. 

Comm Find the line in file 2 that is not in file 1:

Cat/etc/dhcpd.Conf|grep  "fixed-address" |-v ^#|awk ' {print $NF} ' |sed ' s/;//g ' | Sort >/tmp/1.txtarp -n|| grep -v eth0| ' {print $} ' | Sort >/tmp/2.txt-23 2.txt 1.txt               

Note: The contents of two files must be sorted before comparing. Otherwise the result of the output will be wrong.

Method Two: diff command comparison

The diff command is a more classic text comparison tool, and the diff command is more than the Comm parameter. It is often used in combination with patch commands for patch upgrades. It uses the-a parameter by default, which compares the differences between two files per row. Here we want to achieve the desired results, also need to cooperate with grep and awk practical:

2.txt1.txt|" < '|' $ = "" '    

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

Method Three: Awk implements

Awk should be a Rohit in the Shell's common commands, and almost all the other commands can do, and awk can do it (but some may be more complicated to write). In this case, there is no exception:

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

Find the same parts between the two files you can use

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

The following two statements can also be replaced with:

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

Note:

1. When awk is implemented, there is no need to sort the two files in advance,

2. Note that the order in which two files are placed in the two commands is different. Three methods of implementation, the order of the file must be reversed, reversed, the effect just want to reverse, it becomes found in the file 1 in the file 2 does not have the line.

Method Four: grep fraught method

Another bad way to do this online is through the grep command:

--1.txt2.txt  

The results of this method's output are not correct, regardless of whether I have sort in reverse two files beforehand.

Linux compares two files differently (6/21)

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.