Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: change \n to \t

by haukex (Bishop)
on Aug 24, 2019 at 09:12 UTC ( #11104935=note: print w/replies, xml ) Need Help??


in reply to change \n to \t

Note that none of the solutions so far take into account what your original code seems to want to do, which is to only replace newlines before [ATCGN]; OTOH your solution doesn't take into account whether the lines start with > or not. Here is one way that does both, while processing the file line-by-line, thereby saving memory.

use warnings; use strict; my $prevline; while ( my $curline = <DATA> ) { next unless defined $prevline; if ( $prevline=~/^>/ && $curline=~/^[ATCGN]/ ) { $prevline =~ s/\n\z/\t/; } print $prevline; } continue { $prevline = $curline } print $prevline if defined $prevline; __DATA__ >1 AGTCGTAGCAT foo bar >2 TGAGCTACG >3 GGCATAGN quz >4 CGCACNCAGCTACACC >5 NGATAGCTACA

Output:

>1 AGTCGTAGCAT foo bar >2 TGAGCTACG >3 GGCATAGN quz >4 CGCACNCAGCTACACC >5 NGATAGCTACA

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11104935]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2021-01-19 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?