Compare the contents of a file with the same parts and different parts of Linux

Source: Internet
Author: User

Description, is through file2 content and file1 do compare, finally listed file1 and file2 different parts!

First, prepare two documents:

Catcat  file2ce

Second, the comparison method:

Method One:grep command

grep -V-f file2 file1abd

Method Two: Comm command

[Email protected] test1]# Comm-  file1 file2abd

Command explanation:

Comm

Function Description: Compare two ordered files.
Syntax: Comm [ -123][–help][–version][1th file] [2nd file]
Additional notes:
This command compares the differences of two sorted files in a column and displays the results.

If no parameters are specified, the results are divided into 3 rows:
Line 1th is only a column that appears in the 1th file,
Line 2nd is a column that appears only in the 2nd file.
Line 3rd is the column that appears in the 1th and 2nd files.
If the given file name is "-", the comm instruction reads the data from the standard input device.

Method Three: awk command

Print out file1, file2 files, first column number, second column two file contents | Then sort by the second row | Delete duplicate rows (Sort-u can also, but it retains a duplicate) | Sort by the first column of rows | print out the second column
awk ' {print nr,$0} ' file1 file2 | Sort -k2| Uniq 1| Sort -k1| awk ' {print $} '

Or:

This is the same as the above, because it does not need to consider the first column above, because it has not been printed.

awk ' {print $} ' file1 file2 | Sort| Uniq -U

Excerpt from: http://kinggoo.com/diffcommgrepawksortuniq.htm?utm_source=tuicool&utm_medium=referral

Compare the contents of a file with the same parts and different parts of Linux

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.