How do I return the number of lines that appear with a given start?

Asked

Viewed 96 times

2

I wanted to open a document and have it return to me the last number of a line containing '>' as reference. This of all lines that have this '>'.
or that he reads the amount of '>' that existed in the file and returned me in the form that I could put each number of this (1, 2, 3.) in a variable.

The dice appear that way:

'>'VE05.fasta.screen.Contig1 TTTTGTTTTTTTTTTTTTTTTTTTTATTTAATTTTTTTCTTTGGGGGGGG GGAAAATTTTTTTTTCCCTCCCTTCTACAACACAAGAAAAAAAAACTTCC '>'VE05.fasta.screen.Contig2 TTTTGTTTTTTTTTTTTTTTTTTTTATTTAATTTTTTTCTTTGGGGGGGG GGAAAATTTTTTTTTCCCTCCCTTCTACAACACAA

I made that code, but I know it’s incomplete.

open (my @number, '<', @n);

@number = chop (); 

print "Contig's final number:@num";

close @n;

1 answer

3


open ([FILEHANDLE, '[FILE OPENING TYPE]',[DIRECTORY/FILE] );

If the @n array contains the file information read the array directly:

open my $new_file, '>', 'new_sequence_file'
        or die "Not possible open file"; 

for my $row (@n){

  if ( $row =~ /^'>'/){
    print $new_file ++$sequence.$row, "\n";

  }else{
    print $new_file $row, "\n";
  }
}

If you need to read the file directly:

Opening in read mode

open my $fh, '<', '/usr/bin/TESTE/new_sequence_file.txt'
        or die "Not possible open file" 

Creating a new file to treat sequential, but if you want you can overwrite the main file.

open my $new_file, '>>', 'new_sequence_file'
        or die "Not possible open file"; 

for ( my $row = $fh) ){

  if ( $row =~ /^'>'/){
    print $new_file ++$sequence.$row, "\n";

  }else{
    print $new_file $row, "\n";
  }
}

Exit:

1'>'VE05.fasta.screen.Contig1
TTTTGTTTTTTTTTTTTTTTTTTTTATTTAATTTTTTTCTTTGGGGGGGG
GGAAAATTTTTTTTTCCCTCCCTTCTACAACACAAGAAAAAAAAACTTCC
2'>'VE05.fasta.screen.Contig2
TTTTGTTTTTTTTTTTTTTTTTTTTATTTAATTTTTTTCTTTGGGGGGGG
GGAAAATTTTTTTTTCCCTCCCTTCTACAACACAA

If you need to count the batch header containing '>' use the $Sequence variable

Browser other questions tagged

You are not signed in. Login or sign up in order to post.