awk 多文件

2021-03-16  本文已影响0人  猿来是八阿哥

awk 如何运算多个文件中的内容?

GeedeMacBook-Pro:tmp gee$ cat 1.txt
1,zhangsan
2,lisi
3,wangwu
GeedeMacBook-Pro:tmp gee$ cat 2.txt
1,20
2,25
3,5
GeedeMacBook-Pro:tmp gee$ cat 3.txt
1,F
2,M
3,F
awk -F ',' '
    FILENAME=="1.txt"{
        user[$1",name"]=$2;
        if(!($1 in id)){
            id[$1]="a";
        }
    }
    FILENAME=="2.txt"{
        user[$1",age"]=$2;
        if(!($1 in id)){
            id[$1]="a";
        }
    }
    FILENAME=="3.txt"{
        user[$1",sex"]=$2;
        if(!($1 in id)){
            id[$1]="a";
        }
    }
    END{
        for(i in id){
            print i"=["user[i",name"]","user[i",age"]","user[i",sex"]"]";
        }
    }
' 1.txt 2.txt 3.txt
2=[lisi,25,M]
3=[wangwu,5,F]
1=[zhangsan,20,F]
上一篇下一篇

猜你喜欢

热点阅读