简体   繁体   English

在多个csv文件中映射文件并组合行

[英]Map fileds in multiple csv files and combine the rows

The data in my first csv file is: 我的第一个csv文件中的数据是:

ID, name, city
1, John, NYC
2
3
4, Sam, SFO
5 

In second csv file 在第二个csv文件中

ID, name, city
3, Tim, STL
2, Daniel, BOS

Third csv file 第三个csv文件

ID, name, city
5, Eric, AST

I want a single csv file with the aggregated data: 我想要一个带有聚合数据的单个csv文件:

ID, name, city
1, John, NYC
2, Daniel, BOS
3, Tim, STL
4, Sam, SFO
5, Eric, AST 

I'm trying to do this with awk but I'm a beginner so I couldn't figure out a way to do this. 我试图用awk做这个,但我是初学者,所以我想不出办法做到这一点。 Any pointers would be helpful. 任何指针都会有所帮助。

In the output we suppress no-name records and headers, then sort by ID: 在输出中,我们禁止无名称记录和标题,然后按ID排序:

$ (head -1 1st.csv
   awk -F, 'NF > 2 && FNR > 1' {1st,2nd,3rd}.csv | sort -n ) | tee combined.csv

假设CSV中的数据与您在上面共享的数据相同。

cat f1.csv f2.csv f3.csv|awk -F',' '$2!="" && $3!=""'

Please try following in single awk and let me know if this helps you. 请在单个awk中尝试以下操作,并告诉我这是否对您有所帮助。

awk -F, 'NR==1{print;next}FNR>1{a[$1]=NF>1 && a[$1]?a[$1] FS $0:(NF>1?$0:"")} END{for(i in a){if(a[i]){print a[i] | "sort"}}}' 1.csv 2.csv 3.csv

Output will be as follows. 输出如下。

ID, name, city
 1, John, NYC
 2, Daniel, BOS
 3, Tim, STL
 4, Sam, SFO
 5, Eric, AST

It should be working for more than 3 files too, only thing if it crosses limit of open files then in order to avoid any too many files opened error, we have to run following code. 它应该工作超过3个文件,只有它超过打开文件的限制然后为了避免任何too many files opened错误,我们必须运行以下代码。

awk -F, '
NR==1{
  print;
  next
}
FNR==1{
  if(val){
    close(val)
};
  val=FILENAME
}
FNR>1{
  a[$1]=NF>1 && a[$1]?a[$1] FS $0:(NF>1?$0:"")
}
END{
  for(i in a){
    if(a[i]){
     print a[i] | "sort"
}}
}
' 1.csv 2.csv 3.csv
$ cat f1
ID, name, city
1, John, NYC
2
3
4, Sam, SFO
5 

$ cat f2
ID, name, city
3, Tim, STL
2, Daniel, BOS

$ cat f3
ID, name, city
5, Eric, AST

$ awk -F, 'FNR==1{i++}i<3{a[$1+0]=$0;next}i==3 && $1+0 in a{print a[$1+0];next}1' f2 f3 f1
ID, name, city
1, John, NYC
2, Daniel, BOS
3, Tim, STL
4, Sam, SFO
5, Eric, AST

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM