Shell-Script
diff 將每一行標記為舊的、新的或未更改的
我需要區分兩個未排序的文本文件,輸出將每一行標記為舊的、新的或未更改的。標記應該出現在行的開頭或結尾,並且除了這些標記的行之外應該沒有其他輸出。
例如,假設我們有以下兩個文件:
舊的.txt
unchanged line one unchanged line two unchanged line three deleted line one deleted line two deleted line three unchanged line four unchanged line five unchanged line six
新的.txt
unchanged line one unchanged line two unchanged line three unchanged line four unchanged line five inserted line one inserted line two inserted line three unchanged line six
GNU diff 允許您為舊的、新的和未更改的“輸入組”指定列印格式,但這些僅應用於舊/新/未更改行的第一行。例如,執行
diff --old-group-format='- %<' --new-group-format='+ %>' --unchanged-group-format='| %=' old.txt new.txt
結果| unchanged line one unchanged line two unchanged line three - deleted line one deleted line two deleted line three | unchanged line four unchanged line five + inserted line one inserted line two inserted line three | unchanged line six
但是,我需要 diff 看起來像這樣,每一行都被標記:
| unchanged line one | unchanged line two | unchanged line three - deleted line one - deleted line two - deleted line three | unchanged line four | unchanged line five + inserted line one + inserted line two + inserted line three | unchanged line six
我可以單獨使用 GNU diff 還是與其他常見的命令行文本處理工具(如 sed)結合使用來做到這一點?一個攜帶式(POSIX)解決方案可能會更好,只要它不太多毛。
GNU
diff
支持行格式和組格式:$ diff --old-line-format='- %L' --new-line-format='+ %L' --unchanged-line-format='| %L' old.txt new.txt | unchanged line one | unchanged line two | unchanged line three - deleted line one - deleted line two - deleted line three | unchanged line four | unchanged line five + inserted line one + inserted line two + inserted line three | unchanged line six