2017-05-26 6 views
0

私は最初の数行にいくつかの書いたファイルをいくつかの表形式で出力しています。 最初の行を残してから表形式の出力にスキップしたいのですが、単純な音でも問題はあります。特定の行にスキップしてそれを後でパースする方法 - perl

Query   [VOG0001]|NC_002014-NP_040572.1| 1296..1562 + 88 aa|G V protein 
Match_columns 100 
No_of_seqs 7 out of 16 
Neff   2.6 

No Hit        Prob E-value P-value Score SS Cols Query HMM Template HMM 
1 d1gvpa_ b.40.4.7 (A:) Gene V p 100.0 1.6E-38 1.4E-43 221.5 0.0 87 2-89  1-87 (87) 
2 d1gvpa_ b.40.4.7 (A:) Gene V p 100.0 1.6E-38 1.4E-43 221.5 0.0 87 2-89  1-87 (87) 
3 d1gvpa_ b.40.4.7 (A:) Gene V p 100.0 1.6E-38 1.4E-43 221.5 0.0 87 2-89  1-87 (87) 

スクリプトを解析しようとしました::

open (IN, $hhr_report) or die "cannot open $hhr_report\n"; 
while (my $line=<IN>){ 
    if ($line =~/^Query/){ 
      my @query=split(/\|/,$line); 
      my $vogL=$query[0]; 
      my @vogL2=split(/\s+/,$vogL); 
      $vog=$vogL2[1]; 
      $vog=~ s/\[//g; 
      $vog=~ s/\]//g; 
    print "query_array:\[email protected]\n"; 
    print "query_vog:\t$vog\n"; 
    } 
    next until ($line =~/Query HMM/); 
    #next if ($line =~/Query HMM/); 
    #next until ($line =~/^No\s[0-9]+/); 
    print "$line\n"; 
    my @columns = split(/\s+/,$line); 

... }

私は「私の場合よく分からない 私の戦略は、ヘッダ

例入力ファイルを見つけることです しかし、今私はヘッダーライン(containginクエリHMM)を解析しているようですが、私はtその後、行を解析する。

ありがとうございました。

+0

あなたの「次へ」は意味をなさない。また、whileループの終わりを含めるのを忘れてしまった。私はあなたが何をしたいか明確ではありません。あなたはテーブルまでスキップしたいと言っていますが、あなたのコードでは最初の行を読んでいます。これはテーブルに遠く離れていなくても最初の行です。説明してください。 – simbabque

+0

申し訳ありません最初の行を解析し、表形式のセクションにスキップします(上で編集しました)。私は最初の行だけを解析してからテーブルのヘッダーを解析するだけなので、ループ全体を含めなかった。 – user2814482

+1

素人のファイルハンドルではなく、字句変数を使う。 2-argオープンではなく、3-argオープンを使用します。エラーメッセージに '$!'を含めてください。 – melpomene

答えて

1

私は 、ヘッダ行にすべてを破棄(または最初の行を解析)しようとすると、そのようになどのヘッダの後に行を解析し始めるでしょう:

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

open (my $fh, "<", $hhr_report) or die "Cannot open $hhr_report: $!"; 

my $header; 
do { 
    $header = <$fh>; 
    # If you need to parse lines before the header for some reason, 
    # do that here 
}while(!is_header($header)); 

# If you like, parse the header column to get the column names 

my @lines; 

while (my $line = <$fh>){ 

    my @columns = split_line($line); 
    push @lines, \@columns; 

} 

sub is_header { 
    my $line = shift; 

    return $line =~ /^No\sHit/ ? 1 : 0; 
} 

sub split_line { 
    my $line = shift; 
    # Here, use a regex to split the columns, depending on what you need. 
    # You could also consider outputting errors if the line is malformatted or missing important values 

} 
0

私はあなたがしようとしているものだと思います達成はより簡単に行うことができます。私は、あなたがしたい理解:

  1. は、ファイルやプロセスの最初の行を取得し、それ
  2. テーブル
  3. プロセスまで、次の行の表形式データ

もしそうなら、あなたは可能性をスキップ

open (IN, $hhr_report) or die "cannot open $hhr_report\n"; 

# Get the first line of the file and process it: 
my $first_line = <$fh>; 
my @query=split(/\|/,$first_line); 
my $vogL=$query[0]; 
my @vogL2=split(/\s+/,$vogL); 
my $vog=$vogL2[1]; 
$vog=~ s/\[//g; #/ 
$vog=~ s/\]//g; #/ 
print "query_array:\[email protected]\n"; 
print "query_vog:\t$vog\n"; 

# Work on the rest of the file: 
my $in_table = 0; 
while (my $line=<IN>){ 
    if ($in_table) { 
     # process your columns here 
     print "$line\n"; 
     my @columns = split(/\s+/,$line); 
     ... # the rest of your processing 
    } 
    # read (and throw away) lines until you match the table header: 
    $in_table = 1 if $line =~/Query HMM/; 
    # next time through the while loop you'll have your 
    # first tabular data and the $in_table will be true 
} 
関連する問題