如何从perl脚本中复制列并打印出来?

时间:2014-04-04 20:00:20

标签: perl copy

查询:3 SSESVENECMCWAARDPSGLLSPHTITRRSVTTDDVSLTITHCGVCYADVIWSRNQHGDS 62

我需要复制SSESVENECMCWAARDPSGLLSPHTITRRSVTTDDVSLTITHCGVCYADVIWSRNQHGDS并打印出来。怎么样?当然使用perl编程。感谢

>gnl|Liriodendron|b3_c1691

Query: 3   SSESVENECMCWAARDPSGLLSPHTITRRSVTTDDVSLTITHCGVCYADVIWSRNQHGDS 62
Query: 63  KYPLVPGHEIAGIVTKVGPNVQRFKVGDHVGVGTYVNSCRECEYCNEGQEVNCAK-GVFT 121
Query: 122 FNGIDHDGSVTKGGYSSHIVVHERYCYKIPVDYPLESAAPLLCAGITVYAPMMRHNMNQP 181
Query: 182 GKSLGVIGLGGLGHMAVKFGKAFGLSVTVFSTSISKKEEALNLLGAENFVISSDHDQMKA 241
Query: 242 LEKSLDFLVDTASGDHAFDPYMSLLKIAGTYVLVGFPSEIKISPANLNLGMRMLAGSVTG 301
Query: 302 GTKITQQMLDFCAAHKIYPNIEVIPIQKINEALERVVKKDIKYRFVIDIKNSLK 355

这是我现在得到的输出。我想只打印每行的第2列并像这样打印

>gnl|Liriodendron|b3_c1691

SSESVENECMCWAARDPSGLLSPHTITRRSVTTDDVSLTITHCGVCYADVIWSRNQHGDSKYPLVPGHEIAGIVTKVGPNVQRFKVGDHVGVGTYVNSCRECEYCNEGQEVNCAK-GVFTFNGIDHDGSVTKGGYSSHIVVHERYCYKIPVDYPLESAAPLLCAGITVYAPMMRHNMNQPGKSLGVIGLGGLGHMAVKFGKAFGLSVTVFSTSISKKEEALNLLGAENFVISSDHDQMKALEKSLDFLVDTASGDHAFDPYMSLLKIAGTYVLVGFPSEIKISPANLNLGMRMLAGSVTGGTKITQQMLDFCAAHKIYPNIEVIPIQKINEALERVVKKDIKYRFVIDIKNSLK

这是我目前的编码:

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

#This line will ask for file name
print "Entry your BLAST file name \n";

#This line will save the file name into $file
my $file= <>;

#This line will open the input file
open(FILE, "$file");

#This line will open the output file
open(OUT, ">Blust_seq_result.txt");

#This line will create the loop to search the substring
while($file = <FILE>)
{
#This line will search ">gnl"
if($file =~ /^>gnl/)
{
#This line will print all lines containing the character ">gnl" at the beginning
print(OUT "\n$file\n");
}

#This line will search the substring "Query:"
if($file =~ /Query:/)
{

#This line will print all lines containing the substring "Query:"
print(OUT "$file");`enter code here`

}

}

我如何获得该输出?

2 个答案:

答案 0 :(得分:0)

试试这个:

#!/usr/bin/perl

use strict;
use warnings;

print "Entry your BLAST file name \n";
chomp(my $file= <>);

open my $INFILE, "<", "$file";
open my $OUTFILE, ">", "Blust_seq_result.txt";

while (my $line = <$INFILE>) {
    if ($line =~ /^>gnl/) {
        print $OUTFILE "\n$line\n";
    }
    if ($line =~ /^Query:/) {
        print $OUTFILE $line =~ /\s+(\S+)\s+\S+$/;
    }
}

答案 1 :(得分:0)

我会用awk:

awk '/>gnl/{print;next} /^Query/{x=x $3} END{print x}' file

或者如果您喜欢Perl:

perl -lanE 'say if /^>gnl/; $x.=$F[2] if/^Query/; END{say $x}' file