bash 在bash中合并两个csv文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43339859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Merge two csv files in bash
提问by cpwah
I have two csv files.
我有两个 csv 文件。
Columns of the first file
第一个文件的列
col1, col2, col3,......col4095, col4096, col4097
Columns of the second file
第二个文件的列
col1, col2, col3,......,col4095, col4096, col4097
Expected output - the last column of the first file is discarded
预期输出 - 第一个文件的最后一列被丢弃
col1, col2, col3.......,col4095. col4096, col1, col2, col3....col4095, col4096, col4097
Both files have the same number of rows, I want to merge the two files into one file.
两个文件的行数相同,我想将两个文件合并为一个文件。
回答by sat
Use paste
:
使用paste
:
paste -d, f1.csv f2.csv > out.csv
To ignore last column of first file:
忽略第一个文件的最后一列:
awk -F, 'NF-=1' OFS=, f1.csv | paste -d, - f2.csv > out.csv
回答by heemayl
With only awk
:
只有awk
:
awk -F ',[[:blank:]]*' 'NR==FNR {for (i=1;i<NF;i++) out=out$i", " ; next} \
{out=out% cat f1.csv
col1, col2, col3,......col4095, col4096, col4097
% cat f2.csv
col1, col2, col3,......,col4095, col4096, col4097
% awk -F ',[[:blank:]]*' 'NR==FNR {for (i=1;i<NF;i++) out=out$i", " ; next} {out=out##代码##} END{print out}' f1.csv f2.csv
col1, col2, col3, ......col4095, col4096, col1, col2, col3,......,col4095, col4096, col4097
} END{print out}' f1.csv f2.csv
-F ',[[:blank:]]*'
sets the field separator as,
followed by any number of space/tabNR==FNR
will be true for the first file only, then{for (i=1;i<NF;i++) out=out$i", " ; next}
will be executed, which will iterate over, and concatenate all the fields but the last one in variableout
The record of the second file would be concatenated to the variable
out
--{out=out$0}
Finally, the value of variable
out
is printed --END{print out}
-F ',[[:blank:]]*'
将字段分隔符设置为,
后跟任意数量的空格/制表符NR==FNR
将仅对第一个文件为真,然后{for (i=1;i<NF;i++) out=out$i", " ; next}
将被执行,这将迭代并连接所有字段,但变量中的最后一个out
第二个文件的记录将连接到变量
out
——{out=out$0}
最后,
out
打印变量的值——END{print out}
Example:
例子:
##代码##