Perl更改文件的列分隔符

时间:2017-06-16 08:56:36

标签: perl

我将uniq -c的输出存储到两个文件$lfile$lfile2中,我尝试使用" "命令将列分隔符设为tr,但它似乎不起作用,在$line拆分后,$count$e_code中没有任何内容存储。

如何将$line拆分为两部分?

`egrep -o [A-Z]{3}[0-9]{5} $e_file |sort|uniq -c |sort -nrk1 |head -15  >$lfile1`;
`egrep -o [A-Z]{3}[0-9]{5} $y_file |sort|uniq -c |sort -nrk1 |head -150 >$lfile2`;

open (IN, "<$lfile1") ;

foreach $line (<IN>)
{
  my $f_line=`echo $line|tr -s ' ' `  ;
  print "$f_line  \n" ;

  my ($count, $e_code) = split / /, $f_line;

2 个答案:

答案 0 :(得分:0)

uniq -c产生类似于此的输出:

      2 ABC12345
      1 ZXC09876

注意前导空格。显然,您打算剥离前导空格,但保持其间的空间对split / /, $f_line;成功至关重要。

要删除前导空格,只使用^\s+模式(^是行锚的开头)并将其传递给s///替换运算符:

$line =~ s/^\s+//;

请注意,您可以使用纯Perl完成此任务:

my %counts = ();
open(my $fh, $e_file) or die "Failed to open $e_file: $!";
while (<$fh>) {
    # collect counts of each [A-Z]{3}[0-9]{5} match in the %counts
    # hash with the match being a kay in this hash and the number
    # of occurrences of this match being the value
    $counts{$1}++ foreach /([A-Z]{3}[0-9]{5})/g;
}

# iterate through the first 15 top encountered matches
foreach my $key (
    (
        sort {$counts{$b} <=> $counts{$a}} keys %counts   # sort dictionary keys
                                                          # in value descending order
    )[0..14]                           # take first 15 items of the ordered list
)
{
    print "$key $counts{$key}\n";
}

演示:https://ideone.com/eN1AyJ

答案 1 :(得分:-1)

要压缩Perl中的空格,可以使用替换运算符s

$line =~ s/ +/ /g;

或者您可以使用音译操作符tr

$line =~ tr/ //s;

使用split时没有任何问题。