简体   繁体   English

如何使用Perl将列从多个文件复制到同一输出文件

[英]How can I copy columns from several files into the same output file using Perl

This is my problem. 这是我的问题。

I need to copy 2 columns each from 7 different files to the same output file. 我需要将2列从7个不同的文件复制到相同的输出文件。 All input and output files are CSV files. 所有输入和输出文件均为CSV文件。 And I need to add each new pair of columns beside the columns that have already been copied, so that at the end the output file has 14 columns. 我需要在已复制的列旁边添加每对新列,以便最后输出文件具有14列。

I believe I cannot use 我相信我不能使用

open(FILEHANDLE,">>file.csv").

Also all 7 CSV files have nearlly 20,000 rows each, therefore I'm reading and writing the files line by line. 另外,所有7个CSV文件每个都有近20,000行,因此我正在逐行读取和写入文件。

It would be a great help if you could give me an idea as to what I should do. 如果您能给我一个关于我应该做什么的想法,那将是一个很大的帮助。

Thanx a lot in advance. 非常感谢。

Provided that your lines are 1:1 (Meaning you're combining data from line 1 of File_1, File_2, etc): 假设您的行是1:1(意味着您要合并来自File_1,File_2等的第1行的数据):

  • open all 7 files for input 打开所有7个文件进行输入
  • open output file 打开输出文件
  • read line of data from all input files 从所有输入文件读取数据行
  • write line of combined data to output file 将合并数据行写入输出文件

Text::CSV is probably the way to access CSV files. Text :: CSV可能访问CSV文件的方法。

You could define a csv handler for each file (including output), use getline or getline_hr (returns hashref) methods to fetch data, combine it into arrayrefs, than use print . 您可以为每个文件(包括输出)定义一个csv处理程序,使用getlinegetline_hr (返回hashref)方法来获取数据,将其组合为arrayrefs,而不是使用print

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

相关问题 如何使用Perl合并两个文件的某些列? - How can I merge some columns of two files using perl? 如何使用Perl的File :: Copy复制名称中包含特殊字符的文件? - How can I copy files with special characters in their names with Perl's File::Copy? 如何使用 Perl 将 Curl 输出存储到文件中? - How can I store Curl output into a file using Perl? 如何使用Perl的HTML :: TableExtract从HTML文件中提取带有标题名称的特定列 - How can I extract specific columns with header names from an HTML file using Perl's HTML::TableExtract 如何从Perl中的多个文件创建HTML文档? - How can create an HTML document from several files in Perl? 在Perl中,如何将列的子集从XLSX工作表复制到另一个? - In Perl, how can I copy a subset of columns from an XLSX work sheet to another? 如何打印从一个文件中搜索字符串的输出到 Perl 中的输出文件 - How to print the output of seaching a string from a no of files to a output File In Perl 如何复制目录但忽略 Perl 中的某些文件? - How can I copy a directory but ignore some files in Perl? 如何复制除 Perl 中所有隐藏文件之外的目录? - How can I copy a directory except for all of the hidden files in Perl? 如何使用Perl将多个文本文件转换为.html文件格式的相同文件名? - How can i convert multiple text files into same filename with .html files format using perl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM