提取数据子集

1 regex perl parsing r

这似乎是一个简单的任务,但对于编程世界来说是全新的,我遇到以下任务的问题:我有一个巨大的文件,其格式如下:

track type= wiggle name09
variableStep chrom=chr1
34 5 
36 7 
54 8 
variableStep chrom=chr2 
33 4 
35 2 
78 7 
this is text with the word random in it# this we need to remove
82 4 
88 6 
variableStep chrom=chr3 
78 5 
89 4 
56 7
Run Code Online (Sandbox Code Playgroud)

现在我想要的只是一个输出

一个名为1且仅包含的文件

34 5
36 7
54 8

a second file called 2

33 4
35 2
78 7
82 4 
88 6

a third file

78 5
89 4
56 7
Run Code Online (Sandbox Code Playgroud)

能得到一些帮助真是太好了......如果有人知道如何在R中做到这一点......那会更好

Ala*_*avi 5

以下是否有帮助?

#!/usr/bin/env perl

use strict;
use warnings;

my $filename = 1;
my $flag;
my $fh;

while (<>) {
    if (/^\d+\s+\d+\s*$/) {
        if ( $flag == 1 ) {
            $flag = 0;
            open $fh, '>', $filename;
            $filename++;
        }
        print $fh $_;
    }
    elsif (/random/) {
        next;
    }
    else {
        $flag = 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

将上述内容保存为extract(或任何其他名称,如果重要).

假设带有数据的文件已命名file.

perl extract /path/to/file
Run Code Online (Sandbox Code Playgroud)