簡體   English   中英

比較unix中的兩個文件,並將delta添加到一個文件中

[英]Compare two files in unix and add the delta to one file

兩個文件都包含最少2000行的字符串和數字數據。 如何將非重復數據從file2.txt添加到file1.txt 基本上file2有新的數據行,但我們也想確保我們沒有向file1.txt添加重復的行。

  • File1.txt >這是主數據文件
  • File2.txt >此文件包含我們要添加到file1的新數據

謝謝,

將這兩個文件與-u選項一起排序以刪除重復項。

sort -u File1.txt File2.txt > NewFile.txt && mv NewFile.txt File1.txt

如果文件被排序的另一個選項,只是為了有一些選擇(我喜歡comm :))

comm --check-order --output-delimiter=''  -13 File1.txt File2.txt >> File1.txt

您可以使用grep ,如下所示:

# grep those lines from file2 which are not in file1
grep -vFf file1 file2 > new_file2
# append the results to file1
cat new_file2 >> file1

使用awk:

awk '!a[$0]++' File1.txt File2.txt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM