Shell-Script
比較兩個文件並寫入不匹配的行和不匹配的列。指出不匹配的列是我的主要問題陳述
我有一個要求,我需要將兩個文件與每一列進行比較,並在另一個文件中寫入相應的差異以及一些顯示不匹配列的標識。**指出不匹配的列是我的主要問題陳述。**例如,我們有如下文件:
File 1 1|piyush|bangalore|dev 1|piyush|bangalore|QA 2|pankaj|bangalore|dev 3|rohit|delhi|QA File 2 1|piyush|bangalore|QA 1|piyush|bangalore|QA 2|pankaj|bangalore|dev 3|rohit|bangalore|dev
預期的輸出文件看起來有點像。
File 1 1|piyush|bangalore|**dev** File 2 1|piyush|bangalore|**QA** File 1 3|rohit|**delhi**|**QA** File 2 3|rohit|**bangalore**|**dev**
我想實現這樣的事情,我可以看到不匹配的列以及不匹配的行。我試過了
diff File1 File2 > Diff_File
但這只給了我不匹配的記錄或行。**我也沒有辦法指出不匹配的列。**如果可以使用 shell 腳本或 awk 命令,請幫助我,因為我對此很陌生。提前致謝。
Python 3.x 解決方案:
diff_marked.py腳本:
import sys file1_name = sys.argv[1] file2_name = sys.argv[2] with open(file1_name, 'r') as f1, open(file2_name, 'r') as f2: f1_lines = f1.readlines() # list of lines of File1 f2_lines = f2.readlines() # list of lines of File2 for k,l in enumerate(f1_lines): f1_fields = l.strip().split('|') # splitting a line into fields by separator '|' if k < len(f2_lines) and f2_lines[k]: has_diff = False f2_fields = f2_lines[k].strip().split('|') for i,f in enumerate(f1_fields): if f != f2_fields[i]: # comparing respective lines 'field-by-field' between two files f1_fields[i] = '**' + f + '**' # wrapping differing fields f2_fields[i] = '**' + f2_fields[i] + '**' has_diff = True if has_diff: print(f1.name) # print file name print('|'.join(f1_fields)) print(f2.name) print('|'.join(f2_fields))
***用法:(*你可能有另一個python版本,目前案例已經在python 3.5上測試過)
python3.5 diff_marked.py File1 File2 > diff_output
diff_output
內容:File1 1|piyush|bangalore|**dev** File2 1|piyush|bangalore|**QA** File1 3|rohit|**delhi**|**QA** File2 3|rohit|**bangalore**|**dev**