One day a shell command text operation series-comm command usage _linux shell

Source: Internet
Author: User

The comm command compares the data in each row of the two sorted files and displays their results, and if no arguments are specified, the Comm command reads the two files and then generates three columns of output:1> only the rows that appear in the File1 2> rows file2 A row that exists in all two files.
If you specify a-(minus sign) for one of the file names, the Comm command reads the data from the standard input device.

digression : Speaking of comparison files, have been used beyond Compare, the old version, we can only right-click to select the file for comparison, and then select the comparison file, contrast. Now he has a rich interface to support comparing files on Linux. What's it for? When SVN is poorly managed, version control does not work well and you expect him. It's all experience--!

back to the point: in the shell, there is an order---comm. Comm can be used for comparisons between two files. Can be used for file comparisons, intersections, differences, and differential set operations.

This definition is not really detailed enough, it should be comm to compare the sorted file 1 and file 2 line by row.

Name

(ashamed) didn't find the right name. COM is obviously the beginning of compare, M is mode. Look at the master to correct me.

View-help

Usage: Comm [options] ... Document 1 Document 2
Compare sorted file 1 and file 2 line by row.

If the option is not included, the program generates three columns of output. The first column contains rows that are specific to file 1, the second column contains rows that are specific to file 2, and the third column contains rows that are common to two files.

-1 does not output file 1-specific rows
-2 does not output file 2-specific rows
-3 does not output two common rows of files

--check-order Check that the input is sorted correctly, even if all input lines are aligned
--nocheck-order does not check whether the input is sorted correctly
--output-delimiter=str according to STR

Interpretation:

1.--check-order

This command can be--nocheck-order by default if you want to not check the sort.

2.--output-delimiter=str

STR is an expression. can be any non special character

3. -1,-2

Can be written as-12 with a normal command.

Instance:

1. Establishment of A.txt

add element

Apple
Orange
Gold
Silver
Steel
Iron

2. Establishment of B.txt

add element

Orange
Gold
Cookies
Carrot

You can try a direct comparison: Comm A.txt B.txt

You'll notice an error, no sort.

3. Sorting

Copy Code code as follows:

Sort A.txt-o A.txt; Sort B.txt-o B.txt

4. Compare

Copy Code code as follows:

$comm A.txt B.txt

Copy Code code as follows:

Apple
Carrot
Cookies
Gold
Iron
Orange
Silver
Steel

The result is actually 3 columns, where the first column is file 1 unique-1 means to delete the first column

The second column is File 2 special-2 for deleting the second column
The third column represents a common one-3 represents deleting a shared column

4.1 Print Intersection:

$comm –12 A.txt B.txt

4.2 Printing two different rows in a file

$comm –3 A.txt B.txt

Add a small change, two columns are not good-looking, with sed delete the beginning of T

$comm –3 A.txt B.txt | Sed ' s/^\t//'

Actual combat:

Comm in the actual combat, only in some very special scenes to use. It's simpler, not an example. (I encountered the scene, still insist on a better plan, do not use this command, hehe).

This article comes from "Scholar" blog

Related Article

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.