簡體   English   中英

比較兩個文件並獲取相同行的輸出

[英]compare two files and get the output for the same lines

如何通過比較兩個文本文件使用linux命令獲得以下輸出? 謝謝。

文件1:

site110
site120

文件2(更新):

domain1.com - site110
domain2.com - site111
domain3.com - site112
domain4.com - site113
domain5.com - site120
domain6.com - site1201
domain7.com - site1202

輸出:

domain1.com - site110
domain5.com - site120

如果我使用:

grep -f file1 file2

輸出將是:

domain1.com - site110
domain5.com - site120
domain6.com - site1201
domain7.com - site1202

最后兩行不是我想要的。 謝謝。

grep聯機幫助頁

   -f FILE, --file=FILE
          Obtain  patterns  from  FILE,  one  per  line.   The  empty file
          contains zero patterns, and therefore matches nothing.   (-f  is
          specified by POSIX.)

因此:

grep -f file1 file2

domain1.com - site110
domain5.com - site120

使用comm命令。

comm -12 < (sort file1) < (sort file2)

此命令比grep -f更准確。

我想你正在尋找一種數據庫連接功能。 Unix有一個命令: join 在你的情況下:

join -1 1 -2 3 -t " " -o 2.1,2.2,2.3 file1 file2

差異怎么樣?

可能是man paste 可能需要一些輸出處理。

暫無
暫無

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

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