Teach you a method of text comparison under Linux

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

Experienced programmers know that there is a very useful text comparison tool under Windows—BeyondCompare. But it is paid software, and many formal companies do not allow the use of cracked software. Moreover, it can only be used under Windows, there is no Linux version.

The text comparison method introduced in this article does not require any software, only a Linux command. After learning this command, mother will no longer be afraid that you will not be able to compare texts.

This Linux command is the diff command.

diff is a very important tool for Unix systems. It is used to compare the differences between two text files and is one of the cornerstones of code version management.

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

diff [OPTION]... FILES

It's very simple. But it has a lot of options, so many that you doubt life. Let’s leave it alone and learn some of the most commonly used ones. After all, time is spent on the blade.

-b —— Ignore the difference between empty characters in a line (for example, "Hello World!" and "Hello World!!" are considered the same) -B —— Ignore blank lines -i —— Ignore the difference in case -r — — If the directory followed by diff, the files in the subdirectories will be compared recursively.

Let's take a closer look at how to compare texts.

There are three output formats for the diff command:

(1) Normal format (normal diff) (2) Context format (context diff) (3) Combined format (unified diff)

We introduce these three output formats in detail through examples. For example, we now have a file ac, its content is as follows:

Now we make a copy, name it bc, 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 it as follows:

diff a.c b.c

The results of the execution are as follows:

img

Let us explain the meaning of the above picture line by line.

Line by line: 3c3

The first 3 indicates that there is a change in the third line of the file ac, and the following 3 indicates that ac becomes the third line in bc through the change. The c in the middle is the specific change. c means change, other types include d delete (delete) and a increase (addition).

The second line: <hello world!

Indicates that the content of line 3 in the ac file is removed, and the less than sign indicates removal.

The third row:------

Divider

Fourth line:> HELLO world!

Indicates that the content of line 3 is added to the bc file, where the greater than sign indicates an increase.

(2) Context format

Since there are relatively few prompt messages in the normal format, we cannot quickly locate the modified place, and often need to open the file to know the modification details. Therefore, in order to give more information, a context format is introduced. Its use commands are as follows:

diff -c a.c b.c

Among them, c represents context, which means context.

img

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

The first and second lines indicate the file before and after the modification and the update time. The following * 1,4 ** means the content from the first line to the fourth line displayed in the ac file. The exclamation mark (!) before hello world indicates that the line has been changed. If the line is deleted, it is a minus sign (-), and if the line is added, it is a plus sign (+). The meaning of the next few lines is similar.

(3) Combined format

This format is a comprehensive version of the normal format and the context format, and this format is also the format used by git diff. The command using this format is:

diff -u a.c b.c

img

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

The first and second lines indicate the file before and after the modification and the update time. The following -hello world! represents the content in the original file ac, and +HELLO world! represents the content in bc.

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

diff a.c b.c -y -W 50

img

This format is displayed in a side-by-side format, which is also very intuitive. In the third line, there is a "|" symbol, indicating that this line has been changed. In addition, if the front is "<", it means that the following file has one line less content than the previous file; if it is ">", it means that the following file has one line more content than the previous file.

More exciting content, please pay attention to the public number Liang Xu Linux , a public reply to 1024 is available free 5T technical information, including: Linux, C / C ++, Python, raspberry pie, embedded, Java, artificial intelligence , and so on. Reply to the group in the official account and invite you to join the expert cloud technology exchange group.

img

Finally, recently many friends asked me for the Linux learning roadmap , so based on my experience, I spent a month staying up late in my spare time and compiled an e-book. Whether you are in an interview or self-improvement, I believe it will help you! The directory is as follows:

Give it to everyone for free, just ask you to give me a thumbs up!

Ebook | Linux development learning roadmap

I also hope that some friends can join me to make this e-book more perfect!

Gain? I hope the old irons will have a three-strike combo so that more people can read this article

Recommended reading:

Guess you like

Origin blog.csdn.net/yychuyu/article/details/108607257