比較 2 個檔案忽略 @ 和 [ 之間的字串

比較 2 個檔案忽略 @ 和 [ 之間的字串

我正在比較兩個檔案。我試著忽略之前@和之後的字母數字字元[。一條線看起來像

model.Field@d6b0d6b[fieldName

答案1

我會用流程替代這裡:

diff <(sed 's/@[^[]*/@/' old) <(sed 's/@[^[]*/@/' new)

答案2

我假設你是使用 Bash

如果v="model.Field@d6b0d6b[fieldName"那麼你可以執行以下操作:

# Extract the right side of "$v"
r="${v#*[}"
# Extract the left side of "$v"
l="${v%@*}"

# Combine
new_v="$l@[$r"; new_v1="$l$r"

您可以使用“$new_v”或者“$new_v1”取決於您是否想要@和[。


作為韋斯曼先生評論了,我的回答沒有回答問題。沒錯,我沒太注意標題。讓我們修復它並使用以下函數包裝上面的程式碼以根據需要列印單個文件的數據

pf()
{
    while read -r line; do
        # This is a bit fancy but does the same thing as the code above.
        printf '%s\n' "${line%@*}${line#*[}"
    done < "$1"
}

現在,我們可以diff使用以下命令來獲取這兩個檔案:

diff <(pf file1.txt) <(pf file2.txt)

這裡有一個 樣本輸出

rany$ cat file1.txt

model.Field1@__A__[fieldName
model.FieldIAMDIFFERENT@__B__[fieldName
model.Field1@__C__[fieldName

rany$ cat file2.txt

model.Field1@__C__[fieldName
model.Field1@__D__[fieldName
model.Field1@__E__[fieldName

rany$ diff <(pf file1.txt) <(pf file2.txt)

2c2
< model.FieldIAMDIFFERENTfieldName
---
> model.Field1fieldName
rany$

正如您所看到的,@ 和 [ 之間的行不同的事實被忽略,文件之間唯一不同的行是:

model.FieldIAMDIFFERENTfieldName

我很抱歉沒有仔細注意你的標題作為問題的一部分。

答案3

過濾資料檔 - 然後執行 diff -:

sed 's/\@.*\[/@[/' file1 > file1.filt
sed 's/\@.*\[/@[/' file2 > file2.filt
diff file1.filt file2.filt

另一種方法是使用diff有選項 -I 。在 diff 比較中,任何與模式相符的行都會被忽略。選擇一種模式,該模式將唯一選擇不進行比較的行。例如

diff -I 'dataexplorer.bigindex' file1 file2

相關內容