【问题标题】:Concatenate several files连接多个文件
【发布时间】:2017-05-03 15:49:10
【问题描述】:

我需要使用 UTF-8 编码将 20 个 .txt 文件连接成一个。这 20 个文件都在同一个目录中。 你能帮助我吗 ? 我正在考虑类似的事情

use strict;
use warnings;

my $fichier = "*.txt";
    open(FIC,">>:encoding(UTF-8)", $fichier) o
    close (FIC);

<>;

【问题讨论】:

  • cat *.txt &gt; combined_file.txt
  • 文件当前使用的编码是什么?为什么你认为你需要 Perl?
  • 我需要使用 perl,因为它是我唯一知道的语言,哈哈。而且目前的编码是UTF8
  • 因此,如果您使用的是 unixy 系统,请使用@HunterMcMillen 的建议。在 Windows 上,使用 copy /b *.txt combined_file.txt。

标签: perl concatenation


【解决方案1】:

如果输入和输出具有相同的编码,则无需经过解码和重新编码的工作。

print while <>;

例如,

perl -e'print while <>' *.txt >combined_file.txt

或者只是

perl -pe1 *.txt >combined_file.txt

或者只是

cat *.txt >combined_file.txt

如果您不想将通配符作为参数传递,您可以使用以下内容:

my @qfns = glob('*.txt');

然后您可以依次打开每个文件。

【讨论】:

  • 也许在前面加上@ARGV = glob '*.txt';。
  • 可能是@ARGV ||= glob '*.txt'?使其更灵活
  • 在cmd.exe,使用copy /b *.txt combined_file.txt
  • @Hunter McMillen,我想你的意思是@ARGV = glob '*.txt' if !@ARGV;,但这很不合常规。我建议不要这样做
  • @ikegami 是的,这就是我的意思。这只是在脚本默认为所有文本文件通配的情况下,我认为这也不是一个好主意。
猜你喜欢
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 2018-02-18
  • 2021-09-08
  • 2011-09-23
  • 1970-01-01
  • 2016-12-09
  • 1970-01-01
相关资源
最近更新 更多