
我可以比較兩個文字文件,從每行的開頭跳過 N 個符號嗎?
例如文件1:
2018-05-31 12:00:00 This is the first line of text.
2018-05-31 12:00:00 This is the second line of text.
2018-05-31 12:00:00 This is the third line of text.
2018-05-31 12:00:00 This is the forth line of text.
2018-05-31 12:00:00 This is the fifth line of text.
和文件2:
2018-05-31 12:00:01 This is the first line of text.
2018-05-31 12:00:02 This is the second line of text.
2018-05-31 12:00:03 This is the third line of text.
2018-05-31 12:00:04 This is the forth line of text.
2018-05-31 12:00:05 This is the fifth line of text.
如果我逐行比較兩個文件 - 由於時間戳中的秒數,它們是不同的。
但是,如果我跳過兩個文件(日期和時間)中每行開頭的前 19 個符號 - 這些文件是相同的。如何使用 shell 命令(腳本)來做到這一點?
預先非常感謝您。
答案1
使用cut
:
diff <(cut -c 20- file1) <(cut -c 20- file2)
注意:對於 GNU,字元cut
選項-c
實際上適用於位元組而不是字符,但是只要您的輸出以日期/時間戳而不是特殊字元開頭,這應該沒問題。