合并两个 csv 文件以生成第三个 csv 文件,其中包含来自两个现有 csv 文件的选定行 [英] Consolidate two csv file to generate a third csv file with selected rows from both existing csv file

查看:46
本文介绍了合并两个 csv 文件以生成第三个 csv 文件,其中包含来自两个现有 csv 文件的选定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 shell 脚本来生成两个 csv fileA 和 fileB 文件.我需要在现有的 .ksh 文件中添加脚本,它可以将 fileA 的第 3 列添加到要生成的新 csv 文件的第 1 列,将 fileB 的第二列添加到新 csv 的第二列.有什么办法吗?例如.

I have written a shell script which generates the two csv fileA and fileB files. I need to add the script in the existing .ksh file which can add the 3rd column of the fileA to the column 1 of the new csv file that is going to generate and the second column of the fileB to the second column of the new csv. Is there any way to do it ? For example.

fileA                      fileB 

A B C D                    1 2 3 4                

output.csv

C 2 

使用 awk 命令我可以读取如下列:

using the awk command I can read the columns like :

awk -F" " '{print $3}' fileA.csv

但对如何将输出输出到所需的列感到困惑.

But confused how to made the output to the desired columns.

推荐答案

或者如果你喜欢 bash(或 ksh),像这样把 fileB 放在 filedescriptor number 3 上并从每个文件中读取一行:

Or if you like bash (or ksh), put fileB on filedescriptor number 3 like this and read a line from each file:

#!/bin/ksh
while read -r a1 a2 a3 a4 && read -r b1 b2 b3 b4 <&3; do
   echo $a3 $b2 
done < fileA 3<fileB

这篇关于合并两个 csv 文件以生成第三个 csv 文件,其中包含来自两个现有 csv 文件的选定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆