簡體   English   中英

Perl Oneliner解析正則表達式中的多個條件

[英]Perl Oneliner to parse multiple conditions in regex

我有一個文件,其中包含如下所示的行:

>AF001546_1 [88 - 462] 1 MGQQ
>AF001543_1 [88 - 261] ACGT

並不是每行都包含6個OR 5字段。 我想要做的是捕獲字段1,2,3(僅限數字),5(僅限數字)和最后一個字段(ACGT或MGOQ字符串)。

所以預期的輸出是這樣的:

>AF001546_1 88 462 MGQQ
>AF001543_1 88 261 ACGT

現在我使用的perl單行是這個,但失敗了:

perl -lne 'print "$1 $2 $3 $4" if /(\w+)_\d+\D+(\d+)\D+(\d+)\](\D+)/' 

做正確的方法是什么?

perl -lne 'print "$1 $2 $3 $4" if /(>\w+)\D+(\d+)\D+(\d+)\D+\d*\s+(\w+)/'

您還使用以下代碼

use strict;
use warnings;

my $str=">AF001546_1 [88 - 462] 1 MGQQ";

if($str=~/(\w+)\s\D([0-9]{2}) - ([0-9]{3})\D\s\d\s(.*)/)
{
     print "$1 $2 $3 $4\n";
}
while(<>){
 chomp;
 s/\[|\]//g;
 if ($_ =~ /^>/){
    @s = split /\s+/;
    print "$s[0] $s[1] $s[3]\n";
 }    
}

$ perl -F"\s+" -lane '$F[3]=~s/\]//;$F[1]=~s/\[//;print "$F[0] $F[1] $F[3]";' file
>AF001546_1 88 462
>AF001543_1 88 261

試試這個perl -lne'print'$ 1 $ 2 $ 3 $ 4“如果/(\\ w +)_ \\ d + \\ D +(\\ d +)\\ D +(\\ d +)](\\ D +)/ m'

你需要使用修飾符/ m

根據空白的靈活性,這是相當可讀的:

print "$1 $2 $3 $4" if /([^_]+)_\d+ \[(\d+) - (\d+)\] (?:\d+ )?(.*)/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM