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

Teach you a way to achieve text comparison in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Preface

In the process of writing the code, we will inevitably make some modifications to the code. However, there are often changes, so I don't know what the difference between the change and the source file is. Here, we need a text comparison tool for text comparison.

Experienced programmers all know that there is a very useful text comparison tool-BeyondCompare under Windows. But it is paid software, and many regular companies are not allowed to use cracking software. Moreover, it can only be used under Windows, and there is no Linux version.

The text comparison method introduced in this article does not need any software, only a Linux command. Learn this command, and my mother will no longer be afraid that you won't be able to compare texts.

This Linux command is the diff command.

Diff is a very important tool program in Unix system. It is used to compare the differences between two text files and is one of the cornerstones of code version management.

First of all, let's look at its basic command format.

Diff [OPTION]... FILES

It must be very simple. But it has so many options that you doubt life. Let's forget about that for a while, let's first learn some of the most commonly used ones. After all, time is spent on the blade.

-b-- ignore the difference between empty characters on a line (for example, "Hello World!" And "Hello Worldwide!" Think it is the same)

-B-- ignore blank lines

-I-- ignore case differences

-r-if diff is followed by a directory, the files in the subdirectory will be compared recursively

Let's take a specific look at how to make a text comparison.

There are three output formats for the diff command:

(1) normal format (normal diff)

(2) context format (context diff)

(3) merge format (unified diff)

We introduce these three output formats in detail through examples. For example, we now have a file a.c that reads as follows:

Now let's make a copy, name it b.c, and change the lowercase "hello" in line 3 to uppercase "HELLO", as follows:

(1) normal format

In the normal format, we don't need to add any options, just compare them as follows:

The results are as follows:

Let's explain the meaning of the above picture one by one.

One line: 3c3

The first 3 indicates a change in line 3 in the file a.c, and the next 3 means that A. c becomes the third line in b. C through the change. The middle c is the specific change. C stands for change, and other types include d deletion (delete) and an increase (addition).

The second line:

< hello world! 表示a.c文件中去除第3行的内容,其中小于号表示去除。 第三行:------ 分隔线 第四行:>

HELLO world!

Represents the addition of line 3 in the b.c file, where the greater than sign indicates increment.

(2) context format

Due to the lack of prompts in the normal format, we can not quickly locate the changes, and we often need to open the file to know the details of the changes. Therefore, in order to give more information, the context format is introduced. Its usage commands are as follows:

Diff-c a.c b.c

Where c stands for context, which means context.

The specific meaning of the output of the above figure is as follows:

Lines 1 and 2 indicate the file and update time before and after modification. The following * 1pr 4 * represents what the a.c file shows from line 1 to line 4. The exclamation point (!) before hello world indicates that the line has changed, a minus sign (-) if the line is deleted, and a plus sign (+) if the line is increased. The next few lines have similar meanings.

(3) merge format

This format is a combination of the normal format and the context format, and this format is also the format adopted by git diff. The command to use this format is:

Diff-u a.c b.c

The detailed meaning of the output of the above figure is as follows:

Lines 1 and 2 indicate the file and update time before and after modification. In the back-hello world! Represents the contents of the original file a.c, + HELLO world! Represents the contents of b.c.

In addition to the above three formats, there is another way that has always been more intuitive-side-by-side format. The command format for this display format is as follows:

This format is displayed in a side-by-side format, and it is also very straightforward. In line 3, there is a "|" symbol that indicates that the line has changed. In addition, if the front is "", it means that the latter file has one more line than the previous file.

Summary

The above is the whole content of this article, I hope that the content of this article has a certain reference and learning value for your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

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