加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – 如何修剪文件 – 删除具有相同值的列

发布时间:2020-12-15 21:54:01 所属栏目:大数据 来源:网络整理
导读:我希望您通过删除具有相同值的列来修剪文件. # the file I have (tab-delimited,millions of columns)jack 1 5 9john 3 5 0lisa 4 5 7 # the file I want (remove the columns with the same value in all lines)jack 1 9john 3 0lisa 4 7 你能否就这个问题
我希望您通过删除具有相同值的列来修剪文件.
# the file I have (tab-delimited,millions of columns)
jack 1 5 9
john 3 5 0
lisa 4 5 7
# the file I want (remove the columns with the same value in all lines)
jack 1 9
john 3 0
lisa 4 7

你能否就这个问题给我任何指示?我更喜欢sed或awk解决方案,或者可能是perl解决方案.

提前致谢.
最好,

解决方法

#!/usr/bin/perl
$/="t";
open(R,"<","/tmp/filename") || die;
while (<R>)
{
  next if (($. % 4) == 3);
  print;
}

好吧,这是假设它是第三列.如果是按价值:

#!/usr/bin/perl
$/="t";
open(R,"/tmp/filename") || die;
while (<R>)
{
  next if (($_ == 5);
  print;
}

通过问题编辑,OP的愿望变得清晰.怎么样:

#!/usr/bin/perl
open(R,"/tmp/filename") || die;
my $first = 1;
my (@cols);
while (<R>)
{
  my (@this) = split(/t/);
  if ($. == 1)
  {
    @cols = @this;
  }
  else
  {
    for(my $x=0;$x<=$#cols;$x++)
    {
      if (defined($cols[$x]) && !($cols[$x] ~~ $this[$x]))
      {
        $cols[$x] = undef;
      }
    }
  }
  next if (($_ == 5));
#  print;
}
close(R);
my(@del);
print "Deleting columns: ";
for(my $x=0;$x<=$#cols;$x++)
{
  if (defined($cols[$x]))
  {
    print "$x ($cols[$x]),";
    push(@del,$x-int(@del));
  }
}
print "n";

open(R,"/tmp/filename") || die;
while (<R>)
{
  chomp;
  my (@this) = split(/t/);

  foreach my $col (@del)
  {
    splice(@this,$col,1);
  }

  print join("t",@this)."n";
}
close(R);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读