簡體   English   中英

awk比較7個文件,根據Fileds打印匹配和差異:

[英]awk Compare 7 files, print match and difference based on Fileds:

我需要將Ref.txt和Jan.txt的7個文件comapre轉換為Jun.txt並獲取匹配項和不匹配項,在這種情況下,我希望將Ref.txt的Second字段與Jan.txt的所有First字段一起檢查Jun.txt,如果是,則打印Ref.txt(主轉儲)的所有文件,然后將Jan.txt的整個行打印到Jun.txt。 並且在Jan.txt上找不到與Jun.txt匹配的狀態“ NotFound”。

參考文件

abc 10  xxyyzz
bdc 20  xxyyzz
edf 30  xxyyzz
ghi 40  xxyyzz
ofg 50  xxyyzz
mgf 60  xxyyzz

Jan.txt

10  Jan 100
30  Jan 300
50  Jan 500

2月txt

10  Feb 200
20  Feb 400
40  Feb 800
60  Feb 1200

Mar.txt

20  Mar 600
50  Mar 1500

四月

10  Apr 100
30  Apr 300
50  Apr 500

May.txt

10  May 200
20  May 400
40  May 800
60  May 1200

Jun.txt

20  Jun 600
50  Jun 1500

所需輸出:

Ref.txt Ref.txt Ref.txt Jan.txt Jan.txt Jan.txt Feb.txt Feb.txt Feb.txt Mar.txt Mar.txt Mar.txt Apr.txt Apr.txt Apr.txt May.txt May.txt May.txt Jun.txt Jun.txt Jun.txt
abc 10  xxyyzz  10  Jan 100 10  Feb 200 Notfound    Notfound    Notfound    10  Apr 100 10  May 200 Notfound    Notfound    Notfound
bdc 20  xxyyzz  Notfound    Notfound    Notfound    20  Feb 400 20  Mar 600 Notfound    Notfound    Notfound    20  May 400 20  Jun 600
edf 30  xxyyzz  30  Jan 300 Notfound    Notfound    Notfound    Notfound    Notfound    Notfound    30  Apr 300 Notfound    Notfound    Notfound    Notfound    Notfound    Notfound
ghi 40  xxyyzz  Notfound    Notfound    Notfound    40  Feb 800 Notfound    Notfound    Notfound    Notfound    Notfound    Notfound    40  May 800 Notfound    Notfound    Notfound
ofg 50  xxyyzz  50  Jan 500 Notfound    Notfound    Notfound    50  Mar 1500    50  Apr 500 Notfound    Notfound    Notfound    50  Jun 1500
mgf 60  xxyyzz  Notfound    Notfound    Notfound    60  Feb 1200    Notfound    Notfound    Notfound    Notfound    Notfound    Notfound    60  May 1200    Notfound    Notfound    Notfound

預先感謝您的答復

這是禮物:請問一些您不了解的問題

awk '
    FNR == 1 { 
        printf "%s %s %s\t", FILENAME, FILENAME, FILENAME 
        if (NR > FNR) file[++num_files] = FILENAME 
    }
    NR == FNR {
        id[NR] = $2
        ref[NR] = $0
        num_ids++
        next
    }
    { value[FILENAME,$1] = $0 }
    END {
        print ""
        for (row=1; row<=num_ids; row++) {
            printf "%s\t", ref[row]
            for (f=1; f<=num_files; f++) {
                key = file[f] SUBSEP id[row]
                printf "%s\t", (key in value ? value[key] : "Notfound")
            }
            print ""
        }
    }
' {Ref,Jan,Feb,Mar,Apr,May,Jun}.txt
Ref.txt Ref.txt Ref.txt Jan.txt Jan.txt Jan.txt Feb.txt Feb.txt Feb.txt Mar.txt Mar.txt Mar.txt Apr.txt Apr.txt Apr.txt May.txt May.txt May.txt Jun.txt Jun.txt Jun.txt 
abc 10  xxyyzz  10  Jan 100 10  Feb 200 Notfound    10  Apr 100 10  May 200 Notfound    
bdc 20  xxyyzz  Notfound    20  Feb 400 20  Mar 600 Notfound    20  May 400 20  Jun 600 
edf 30  xxyyzz  30  Jan 300 Notfound    Notfound    30  Apr 300 Notfound    Notfound    
ghi 40  xxyyzz  Notfound    40  Feb 800 Notfound    Notfound    40  May 800 Notfound    
ofg 50  xxyyzz  50  Jan 500 Notfound    50  Mar 1500    50  Apr 500 Notfound    50  Jun 1500    
mgf 60  xxyyzz  Notfound    60  Feb 1200    Notfound    Notfound    60  May 1200    Notfound    

暫無
暫無

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

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