awk 多文件
2021-03-16 本文已影响0人
猿来是八阿哥
awk 如何运算多个文件中的内容?
- 文件1:1.txt (id,name)
GeedeMacBook-Pro:tmp gee$ cat 1.txt
1,zhangsan
2,lisi
3,wangwu
- 文件2:2.txt (id,age)
GeedeMacBook-Pro:tmp gee$ cat 2.txt
1,20
2,25
3,5
- 文件3:3.txt (id,sex)
GeedeMacBook-Pro:tmp gee$ cat 3.txt
1,F
2,M
3,F
- 问题:按id聚合,每行输出一个用户信息?
- 答:
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]