Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How linux compares different files

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how linux compares different files". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Linux compare files in different ways: 1, use the comm command to compare sorted files, syntax "comm [options] file name 1 file name 2"; 2, use the diff command, will compare the similarities and differences of the text file line by line, syntax "diff [option] file name 1 file name 2".

The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

Linux comparison files are different

The Linux file comparison command is used to compare the difference between the contents of the two files. There are mainly comm command and diff command. If you want to compare two ordered files, you can use the comm command. If you want to compare two text files line by line, you can use the diff command to list the differences.

Use the comm command to compare:

Compare files that have been sorted

Comm [options] filename 1 filename 2

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

The first column is a line found only in file1; the second column is a line found only in file2; and the third column is a common line for both files.

Parameters.

The parameters file1 and file2 are the pathnames of the files to be compared by comm. When replacing file1 or file2 with a hyphen (-), comm reads input information from standard input

Option

-1 does not display column 1 (that is, rows found only in file1)

-2 does not display the second column (that is, rows found only in file2)

-3 does not display column 3 (that is, lines found in both files are not displayed)

Be careful

If the files are not sorted, comm will not work properly

No row in the second column is preceded by a tab, and each row in the third column is preceded by two tabs

Example:

Original document

$cat abbbbbcccccdddddeeeeefffff$ cat baaaaadddddeeeeeggggghhhhh

Enter the command:

$comm ab aaaaabbbbbccccc ddddd eeeeefffff ggggg hhhhh$

Compare sorted an and b files

$comm-12 a b dddddeeeee

Show only public lines

Use the diff command to compare:

The diff (difference) command is often used to compare the contents of two files. The diff command compares the differences between the two files in the simplest case. If you use "-" instead of the File parameter, the content to be compared will come from standard input.

The diff command compares the similarities and differences of a text file line by line. If the command specifies that a directory comparison is made, files in that directory with the same file name will be compared without any comparison of their subdirectory files.

Compare the format of the file

Diff [options] filename 1 filename 2

Example:

Compare the differences between the two files.

Create two sample files, named text and text1, and enter the following:

/ / test11223344556677//test11122aa334455bb66

Enter the command on the terminal page as follows:

[root@bogon ~] # diff test test13c3 aa7c7 bb9,10d8

< 77 表示第二个文件,第一个文件中需要添加的内容为 aa,该内容存放在第二个文件中; ">

Bb ": > indicates the second file. The content to be added in the first file is bb, which is stored in the second file.

"9 refine 10d8": deletes lines 9 and 10 in the first file to match line 8 in the second file

"

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report