1

我在该文件中有一个文件,如下所示

TEST_4002_sample11_1_20110531.TXT
TEST_4002_sample11_2_20110531.TXT
TEST_4002_sample11_4_20110531.TXT
TEST_4002_sample11_5_20110531.TXT
TEST_4002_sample11_6_20110531.TXT
TEST_4002_sample10_1_20110531.TXT
TEST_4002_sample10_2_20110531.TXT
TEST_4002_sample10_4_20110531.TXT
TEST_4002_sample10_5_20110531.TXT

如果该文件序列的第四个文件丢失,我想要输出,然后打印上一个文件名和下一个文件名作为输出。

TEST_4002_sample11_2_20110531.TXT
TEST_4002_sample11_4_20110531.TXT
TEST_4002_sample10_2_20110531.TXT
TEST_4002_sample10_4_20110531.TXT
4

5 回答 5

1

这个 awk 变体似乎产生了所需的输出:

awk -F_ '$4>c+1{print p"\n"$0}{p=$0;c=$4}'
于 2011-06-10T13:32:43.263 回答
1

简单的perl方式:

perl -F_ -lane 'print "$o\n$_" if $F[3]-$n>1;$o=$_;$n=$F[3]' < file
于 2011-06-12T00:08:44.327 回答
0

在 Perl 中,您可以执行以下操作:

use strict;
use warnings;

my $prev_line;
my $prev_val;

while(<>){
    # get the 4th value
    my $val = (split '_')[3];

    # skip if invalid line
    next if !defined $val;

    # print if missed sequence
    if(defined($prev_val) && $val > $prev_val + 1){
        print $prev_line . $_;
    }

    # save for next iteration
    $prev_line = $_;
    $prev_val = $val;
}

将其保存在 foo.pl 中并使用以下内容运行它:

cat file.txt | perl foo.pl

我敢肯定它可以缩短很多。如果所有行都有效,可以使用这样的东西:

perl -n -e '$v=(/[^_]/g)[3];print"$l$_"if$l&&$v>$p+1;$p=$v;$l=$_' file.txt

或者

perl -naF_ -e '$v=$F[3];print"$l$_"if$l&&$v>$p+1;$p=$v;$l=$_' file.txt
于 2011-06-10T11:12:42.123 回答
0

据我了解您需要什么,这是一个完成这项工作的 Perl 脚本:

#!/usr/local/bin/perl 
use strict;
use warnings;

my $prev = '';
my %seq1;
while(<DATA>) {
    chomp;
    my ($seq1, $seq2) = $_ =~ /^.*?(\d+)_(\d+)_\d+\.TXT$/;
    $seq1{$seq1} = $seq2 - 1 unless exists $seq1{$seq1};
    if ($seq1{$seq1}+1 != $seq2) {
        print $prev,"\n",$_,"\n";
    }
    $prev = $_;
    $seq1{$seq1} = $seq2;
}


__DATA__
TEST_4002_sample11_1_20110531.TXT
TEST_4002_sample11_2_20110531.TXT
TEST_4002_sample11_4_20110531.TXT
TEST_4002_sample11_5_20110531.TXT
TEST_4002_sample11_6_20110531.TXT
TEST_4002_sample10_1_20110531.TXT
TEST_4002_sample10_2_20110531.TXT
TEST_4002_sample10_4_20110531.TXT
TEST_4002_sample10_5_20110531.TXT

输出:

TEST_4002_sample11_2_20110531.TXT
TEST_4002_sample11_4_20110531.TXT
TEST_4002_sample10_2_20110531.TXT
TEST_4002_sample10_4_20110531.TXT
于 2011-06-10T11:39:40.070 回答
0

我曾经glob获取文件(可能就像 一样简单<TEST_*.TXT>)。

use strict;
use warnings;

my %last = ( name => '', group => '', seq => 0 );

foreach my $file ( sort glob('TEST_[0-9][0-9][0-9][0-9]_sample[0-9][0-9]_[0-9]_*.TXT')
    ) {
    my ( $group, $seq ) = $file =~ m/(\d{4,}_sample\d+)_(\d+)/;
    if ( $group eq $last{group} && $seq - $last{seq} > 1 ) { 
        print join( "\n", $last{name}, $file, '' );
    }
    @last{ qw<name group seq> } = ( $file, $group, $seq );
}
于 2011-06-10T13:47:08.727 回答