首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >awk比较7个文件,打印匹配和基于文件的差异:

awk比较7个文件,打印匹配和基于文件的差异:
EN

Stack Overflow用户
提问于 2014-01-27 12:57:06
回答 1查看 112关注 0票数 0

我需要将7个文件( Ref.txt和Jan.txt )包含到Jun.txt,并获得匹配和非匹配,在这种情况下,我希望用Jan.txt到Jun.txt的第一个字段检查Ref.txt的第二个字段,如果是,则打印Ref.txt (主转储)的所有文件,然后将Jan.txt的整个行打印到Jun.txt。如果没有在Jan.txt上找到与Jun.txt匹配的语句,则声明"NotFound“。

Ref.txt

代码语言:javascript
复制
abc 10  xxyyzz
bdc 20  xxyyzz
edf 30  xxyyzz
ghi 40  xxyyzz
ofg 50  xxyyzz
mgf 60  xxyyzz

Jan.txt

代码语言:javascript
复制
10  Jan 100
30  Jan 300
50  Jan 500

Feb.txt

代码语言:javascript
复制
10  Feb 200
20  Feb 400
40  Feb 800
60  Feb 1200

Mar.txt

代码语言:javascript
复制
20  Mar 600
50  Mar 1500

Apr.txt

代码语言:javascript
复制
10  Apr 100
30  Apr 300
50  Apr 500

May.txt

代码语言:javascript
复制
10  May 200
20  May 400
40  May 800
60  May 1200

Jun.txt

代码语言:javascript
复制
20  Jun 600
50  Jun 1500

期望产出:

代码语言:javascript
复制
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

谢谢您的答复

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-27 16:26:13

这是一份礼物:如果你不明白的话,请问一些问题

代码语言:javascript
复制
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
代码语言:javascript
复制
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    
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21381521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档