Teach you a trick to achieve text comparison in Linux

Teach you a trick to achieve text comparison in Linux

Preface

In the process of writing code, we will inevitably make some modifications to the code. But it often happens that when you are making changes, you don’t know what the difference is between the modified file and the source file. Here, we need a text comparison tool to perform text comparison.

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

The text comparison method introduced in this article does not require any software, only a Linux command. Once you learn this command, I will no longer be worried about you not knowing how to do text comparison.

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, let's look at its basic command format.

diff [OPTION]... FILES

It's very simple. But it has so many options that you will question your life. Let’s not worry about that for now and learn the most commonly used ones first. After all, time should be spent wisely.

-b - Ignore differences in blank characters within a line (e.g. "Hello World!" is considered the same as "Hello World!!")

-B — ignore blank lines

-i - Ignore case differences

-r —— If diff is followed by a directory, it will recursively compare the files in the subdirectories.

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

The diff command has three output formats:

(1) Normal format (normal diff)

(2) Context diff

(3) 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 normal format, we don't need to add any options, just compare as follows:

The results after execution are as follows:

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

Row: 3c3

The first 3 indicates that the third line of file ac has changed, and the following 3 indicates that ac has changed to become the third line of bc. The c in the middle is the specific change. c stands for change, and other types include d for deletion and a for addition.

Line 2: < hello world!

It means to remove the content of line 3 in the ac file, where the less than sign indicates removal.

Third line: ------

Divider

Line 4: > HELLO world!

It means adding the content of line 3 to the bc file, where the greater than sign indicates increase.

(2) Context format

Because there is relatively little prompt information in the normal format, we cannot quickly locate the modified area and often need to open the file to know the modification details. So, in order to give more information, the context format was introduced. Its usage command is as follows:

diff -c ac bc

Among them, c stands for context, which means context.

The specific meaning of the output results in the above figure are as follows:

The first and second lines indicate the files before and after modification and the update time. The following *** 1,4 **** indicates the contents of the ac file starting from line 1 to line 4. 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 following lines have similar meanings.

(3) Merge format

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

diff -u ac bc 

The detailed meaning of the output results in the above figure are as follows:

The first and second lines indicate the files before and after modification and the update time. The -hello world! at the end refers to the content in the original file ac, and +HELLO world! refers to the content in bc.

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

This format is displayed in a parallel format, which is also very intuitive and clear. In line 3, there is a “|” symbol, indicating that this line has been changed. In addition, if the leading character is “<”, it means that the following file has one less line of content than the previous file; if it is ">", it means that the following file has one more line of content than the previous file.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • One shell command a day Linux text content operation series - awk command detailed explanation
  • One shell command a day Linux text content operation series - cut command detailed explanation
  • Use Linux regular expressions to flexibly search for text in files
  • One shell command a day Linux text operation series - touch command usage
  • One shell command a day Linux text operation series - head, tail command detailed explanation
  • One shell command a day Linux text operation series - detailed explanation of diff command
  • One shell command a day Linux text content operation series - grep command detailed explanation
  • One shell command a day Linux text operation series - detailed explanation of wc command
  • Detailed explanation of Linux text file and WIN text file line break format conversion command
  • One shell command text operation series a day - Linux dd usage tutorial

<<:  How to upgrade MySQL 5.6 to 5.7 under Windows

>>:  Vue2.x - Example of using anti-shake and throttling

Recommend

Share 13 basic syntax of Typescript

Table of contents 1. What is Ts 2. Basic Grammar ...

base target="" controls the link's target open frame

<base target=_blank> changes the target fram...

How to use VirtualBox to simulate a Linux cluster

1. Set up HOST on the host Macbook The previous d...

Detailed explanation of the difference between CSS link and @import

How to add css in html? There are three ways to s...

Several CSS3 tag shorthands (recommended)

border-radius: CSS3 rounded corners Syntax: borde...

Analysis of GTK treeview principle and usage

The GtkTreeView component is an advanced componen...

MySQL Optimization Summary - Total Number of Query Entries

1. COUNT(*) and COUNT(COL) COUNT(*) usually perfo...

Detailed explanation of how to reduce memory usage in MySql

Preface By default, MySQL will initialize a large...

Linux common text processing commands and vim text editor

Today, let's introduce several common text pr...

Detailed explanation of common commands in Docker repository

Log in docker login Complete the registration and...

How to import Chinese data into csv in Navicat for SQLite

This article shares with you the specific method ...

Detailed code for implementing 3D tag cloud in Vue

Preview: Code: Page Sections: <template> &l...