Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
保存包含';它已通过ActivePerl运行_Perl - Fatal编程技术网

保存包含';它已通过ActivePerl运行

保存包含';它已通过ActivePerl运行,perl,Perl,这一定是个基本问题,但我找不到满意的答案。我这里有一个脚本,用于将CSV格式的数据转换为TSV。我以前从未使用过Perl,我需要知道如何保存Perl脚本运行后打印的数据 脚本如下: #!/usr/bin/perl use warnings; use strict; my $filename = data.csv; open FILE, $filename or die "can't open $filename: $!"; while (<FILE>) { s/"//g; s/,

这一定是个基本问题,但我找不到满意的答案。我这里有一个脚本,用于将CSV格式的数据转换为TSV。我以前从未使用过Perl,我需要知道如何保存Perl脚本运行后打印的数据

脚本如下:

#!/usr/bin/perl

use warnings;
use strict;

my $filename = data.csv;
open FILE, $filename or die "can't open $filename: $!";
while (<FILE>) {
s/"//g;
s/,/\t/g;
s/Begin\.Time\.\.s\./Begin Time (s)/;
s/End\.Time\.\.s\./End Time (s)/;
s/Low\.Freq\.\.Hz\./Low Freq (Hz)/;
s/High\.Freq\.\.Hz\./High Freq (Hz)/;
s/Begin\.File/Begin File/;
s/File\.Offset\.\.s\./File Offset (s)/;
s/Random.Number/Random Number/;
s/Random.Percent/Random Percent/;
print;
}
#/usr/bin/perl
使用警告;
严格使用;
my$filename=data.csv;
打开文件,$filename或死亡“无法打开$filename:$!”;
而(){
s/“//g;
s/,/\t/g;
开始\时间\秒\开始时间/;
结束\时间\秒\结束时间/;
s/低频\赫兹\低频(赫兹)/;
s/高频\赫兹\高频(赫兹)/;
s/Begin\.File/Begin File/;
文件\偏移量\.\.s\/文件偏移量/;
s/Random.Number/Random Number/;
s/Random.Percent/Random Percent/;
印刷品;
}
所有分析的数据都在cmd提示符中。如何保存这些数据

编辑: 谢谢大家!它工作得非常好!从您的cmd提示符:

perl yourscript.pl > C:\result.txt

在这里,您运行perl脚本并将输出重定向到名为result.txt的文件。将CSV文件中的所有逗号视为字段分隔符总是有潜在危险的。CSV文件还可以包含嵌入数据中的逗号。下面是一个示例

1,"Some data","Some more data"
2,"Another record","A field with, an embedded comma"
在代码中,
s/,/\t/g
行对所有制表符都一视同仁,最后一个字段中嵌入的逗号也将扩展为制表符。这可能不是您想要的

下面是一些使用Text::ParseWords正确执行此操作的代码

#!/usr/bin/perl

use strict;
use warnings;

use Text::ParseWords;

while (<>) {
  my @line = parse_line(',', 0, $_);
  $_ = join "\t", @line;

  # All your s/.../.../ lines here

  print;
}
!/usr/bin/perl
严格使用;
使用警告;
使用Text::ParseWords;
而(){
my@line=parse_行(',',0,$);
$\uux=连接“\t”@line;
#所有的s/../../../行都在这里
印刷品;
}

如果运行此操作,您将看到最后一个字段中的逗号没有更新。

您有多确定原始文件中的字段没有嵌入逗号?您的
s/,/\t/g
将对这些字段产生恶劣影响。我不太清楚您的意思,但我的原始数据文件是一个csv,其中嵌入了逗号。我基本上需要我的意思是,CSV文件中并非所有的逗号都是字段分隔符。有些逗号可能出现在字段中,而您的代码无法正确处理它们。我将写一个答案来详细解释。