in reply to Re^2: writing array element to a file
in thread writing array element to a file
So you have a series of lines that look something like this
> 22 GATTGATGCC... > 2 GATGGATGTG... > 26 GATGCATGAT... > 52 GATGATGTGG...
And in your output file you just want the numbers.
The split we had in the original code splits each line on spaces into the @vettore array. If we only want to print the second element of this array (the number) then we do not need the foreach loop. We can alter our print to directly address the second element of the arrayprint $out "$vettore[1]\n"; (array indexing starts at 0). Here is our new line processing block:
while (my $line=<$in>) { if($line=~/^>/) { my @vettore=split(/\s+/, $line); print $out "$vettore[1]\n"; } }
For fun it can also be done as a one liner. Here I added a bit more checking of the line to ensure it had some GATC characters following the number
perl -nle "if (/^>\s+(\d+)\s+[GATC]+/) {print $1}" rep_set_ass_tax.fna >> seq_id.txtIf you are not on windows you may need to change the two " quotes to ' quotes.
Cheers,
R.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: writing array element to a file
by francesca1987 (Initiate) on Apr 25, 2013 at 13:22 UTC | |
by Random_Walk (Prior) on Apr 25, 2013 at 14:31 UTC | |
by francesca1987 (Initiate) on Apr 25, 2013 at 14:38 UTC |