使用Perl的Excel列文件管理器

时间:2018-07-11 10:18:28

标签: regex perl

我有一个.csv文件,其中包含以下数据

   COLUMN 1       COLUMN 2
      STATUS        ID
      Pending       123
       Completed    456
      Expired       789
       Completed    987
        Open        654
        Closed      321
        Completed   159
         Rejected   753

perl脚本应从命令行获取文件名,并且第一列,即应为所有“已完成”过滤掉“状态”,并应在第二列(即,从ID列)中打印值。

输出应类似于:

456 987 159

2 个答案:

答案 0 :(得分:1)

我已从注释中提取了代码,并将其转储到了注释中。将来请在您的问题(可读的地方)中输入。但是您提供给我们的代码无法编译。在运行它之前,我必须进行一些修复。请不要浪费时间给我们看破代码!

这是您现有的代码:

use strict;
use warnings;
use 5.010;

my($input, $line);

if(@ARGV!=1){
  # I've added missing quote marks and a closing semicolon here
  print "\nUsage: <Nmae.pl> <Inputfile Name>";
} # I've added what seems to be a missing close brace here

$input = shift(@ARGV);
# I've added a missing semi-colon here.
open(IN_FILE , "<$input") or die ("Error opening file $input");

while(<IN_FILE>){ # Added missing file input operator around file handle
  $line = $_;
  chomp($line);
  $line =~ s/ //g;
  say $line; # Added missing comma
}

清楚地编写了此代码来处理文本文件。因此,我不确定您为什么在问题中提到Excel。但是我认为您的代码比您的问题更正确,并且已经从文本文件中读取了数据。

因此,这是我将其重写以执行您要求的操作的方式:

use strict;
use warnings;
use feature 'say';

# Errors should go to STDERR, not STDOUT. Also kill the process
# if we have no input. Hence "die", not "print".
die "Usage: <Nmae.pl> <Inputfile Name>\n" unless @ARGV;

# Place to store data we're interested in
my @matches;

# Use empty file input operator to read from files named in @ARGV
# without having to open them.
while (<>) {
  # Look for lines containing "Completed" and capture the
  # following digits
  if (/Completed\s+(\d+)) {
    # Matched digits will be in $1
    push @matches, $1;
  }
}

# Print the matches
say "@matches";

答案 1 :(得分:-1)

我修改了您的脚本。看来您正在加载纯文本文件,而不是Excel文件。

这应该有效:

use strict;
use warnings;

@ARGV or die "\nUsage: <Nmae.pl> <Inputfile Name>;";

my $input = shift;
open my $fh, '<', $input
  or die "Error opening file $input; $!";

local $/ = "\r";
my @lines;
while (my $line = <$fh>) {
  # If the line is: Begins with none or more spaces,
  # "Completed" word, none or more spaces, digits
  # then print captured digits
  $line =~ m{ ^ \s* Completed \s+ (\d+) }sxmi
    and print $1 . "\n"; # $1 stores first capturing group ()
}

输出:

D:\Temp>type aaa.txt
STATUS     ID
Pending     123
Completed   456
Expired      789
Completed   987
Open           654
Closed        321
Completed   159
Rejected    753

D:\Temp>Nmae.pl aaa.txt
456
987
159

编辑:根据@DEV原始脚本更改了答案