Linux sdiff 命令示例


作為 Linux 使用者,我們發現“sdiff”命令是一個非常有用的實用程式,用於互動式比較和合並兩個檔案。它提供了檔案的並排比較,並突出顯示差異,使我們能夠輕鬆識別檔案差異之處以及需要進行哪些更改。

在本文中,我們將引導您逐步瞭解如何在 Linux 中使用“sdiff”命令比較和合並檔案。我們將介紹基本用法、選項以及 sdiff 命令可以發揮作用的場景。無論您是經驗豐富的 Linux 使用者還是剛剛入門,本指南都將幫助您熟悉此實用命令,並在工作中充分利用它。

語法 

Linux 中“sdiff”命令的語法。

sdiff [options] file1 file2

我們可以看到“file1”和“file2”是您想要比較和合並的兩個檔案的名稱。

Linux 中兩個檔案的差異

如果我們想要比較和合並 Linux 中的兩個檔案,一種方法是使用 sdiff 命令。要使用它,我們只需將兩個檔案的名稱作為引數寫入命令即可。輸出將並排顯示兩個檔案之間合併後的差異。

輸入

sdiff file1.txt file2.txt

輸出

This is some text in file1.    | This is some text in file2.
It has a few lines.            | It also has a few lines.
These lines are the same.      | These lines are slightly different.
This is the end of file1.      < This is the end of file2.

將所有檔案視為文字檔案

sdiff 的“-a”標誌允許將所有檔案視為文字並逐行進行並排比較,忽略任何非文字字元。當比較可能不是標準文字格式的檔案時,這很有用。

輸入

$ sdiff -a file1.txt file2.txt

輸出

Hello world                             | Hello there

忽略製表符和空白

當我們處理包含過多空白的檔案時,我們可以使用“-W”命令指示 sdiff 在比較時忽略所有空白。

輸入

sdiff -W file1.txt file2.txt

輸出

This is a line with extra spaces.   | This is a line with extra spaces.
This line is spaced out.            | This line is spaced     out.  

使用 sdiff 的“-z”選項可以在比較檔案時忽略每行末尾的尾隨空白,防止錯誤地標記差異,並幫助我們專注於每行的實際內容。

輸入

$ sdiff -z file1.txt file2.txt

輸出

file1.txt                 |  file2.txt
--------------------------|--------------------------
This is line 1.           |  This is line 1.
This is line 2.           |  This is line 2.
This is line 3.           |  This is a different line 3.^M\0
This is line 4.           |  This is line 4.

sdiff 中的“-E”標誌可以忽略由於製表符展開導致的差異,將製表符視為空格,並僅關注每行的實際內容。當比較包含製表符分隔資料的檔案時,這很有幫助。

輸入

$ sdiff -E file1.txt file2.txt

輸出

This is line 3.       |  This is a different line 3.

比較差異時忽略大小寫

sdiff 中的“-i”選項在比較時忽略文字大小寫,使我們能夠更輕鬆地識別具有不同大小寫樣式的檔案之間的實際差異。

輸入

$ sdiff -i file1.txt file2.txt

輸出

This is line 1.                  |  this is line 1.

比較差異時忽略空行

使用 sdiff 的“-B”忽略比較檔案時的空行,使我們能夠更輕鬆地識別內容行之間的實際差異,尤其是在檔案具有不同數量的空行時。

輸入

$ sdiff -B file1.txt file2.txt

輸出

This is line 1.                  |  This is line 1.
This is line 2.                  |  This is line 2.
This is line 3.                  |  This is line 3.
This is line 4.                  |  This is line 4.

定義要輸出的列數

使用“-w”開關自定義 sdiff 在檔案比較期間列印的列數。預設值為 130 列,但我們可以在“-w”選項後指定其他值。例如,“sdiff -w 80 file1.txt file2.txt”僅列印 80 列。當比較包含長行的檔案時,這很有用,可以確保輸出格式化且易於閱讀。

輸入

$ sdiff -w 150 file1.txt file2.txt

輸出

this is line 1			this is line 1
this is line 2			this is a modified line 2
this is line 3			this is line 3
this is line 4			this is line 4
this is line 5			this is line 5
this is line 6			this is line 6
this is line 7			this is line 7
this is line 8			this is line 8
this is line 9			this is line 9
this is line 10			this is line 10
this is line 11			this is line 11
this is line 12			this is line 12
this is line 13			this is line 13
this is line 14			this is line 14
this is line 15			this is line 15
this is line 16			this is line 16
this is line 17			this is line 17
this is line 18			this is line 18
this is line 19			this is line 19
this is line 20			this is line 20

將製表符擴充套件為空格

sdiff 中的“-t”選項可以在輸出中用空格替換製表符,從而更準確地表示檔案之間的差異。當檔案之間的空格數很重要時,這很有用。使用命令“sdiff -t file1.txt file2.txt”將檔案中的所有制表符轉換為輸出中的空格。

輸入

$ sdiff -t file1.txt file2.txt

輸出

This line contains \t a tab. | This line contains       a tab.
This is line 2.              | This is line 2.
This line has a \t tab.      | This line has a             tab.
This line has no tab.        | This line has no tab.

互動式執行 Sdiff

使用 sdiff 的“-o”標誌允許我們將命令的輸出儲存到檔案中。例如,“sdiff -o sdiff.txt file1.txt file2.txt”會將輸出傳送到名為“sdiff.txt”的檔案。使用此標誌,我們可以互動式執行 sdiff,並在按下 Enter 鍵後出現的選單中快速識別兩個檔案之間的差異。這對於複雜的比較尤其有用,在這些比較中,差異可能難以發現。

輸入

$ sdiff file1.txt file2.txt -o sdiff.txt

輸出

This is the first line of file1. | This is the first line of file2.
This is the third line of file1. <
                               > This is a new line in file2.                     

呼叫另一個程式來比較檔案

“--diff-program”開關允許我使用不同的命令列工具來比較檔案,而不是 sdiff。這提供了更大的靈活性,並允許我們選擇更適合用例的工具,例如用於大型檔案的“meld”。它擴充套件了檔案比較的功能,超出了 sdiff 的功能。

輸入

$ sdiff --diff-program=diff file1.txt file2.txt

輸出

1,3c1
< This is the content of file 1
< which has multiple lines
< and some text in them
---
> This is the content of file 2
4,6c2
< and some more text in them
< which is only in file 1
< and not in file 2
---
> and some extra text in file 2

結論

總之,“sdiff”命令是 Linux 命令列工具包中一個非常有價值的工具,使使用者能夠比較兩個檔案或目錄並並排檢視它們的差異。此功能對於識別配置檔案中的更改或對程式碼進行故障排除特別有用。透過掌握 sdiff 命令,使用者可以快速準確地比較和分析其檔案的不同版本,從而有助於防止錯誤並最佳化系統性能。其效率和準確性使其成為任何使用 Linux 的人員的必備工具。

更新時間: 2023年7月28日

398 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.