Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: modifying a file with regex!

by Marshall (Abbot)
on Mar 16, 2012 at 22:19 UTC ( #960080=note: print w/replies, xml ) Need Help??

in reply to modifying a file with regex!

There is no need to substitute anything. Capture what is necessary and re-format the ">" line.
No need to be overly tricky when a couple of straight-forward lines of code will do.
#!/usr/bin/perl -w use strict; my $ID = 1; while (<DATA>) { # this regex captures the trailing number if # the line starts with a ">" # the .*? means a "minimal match" of anything while # allowing the rest of the regex to succeed. # the \n is counted as white space, a \s* character # if (my ($number) = $_ =~ /^>.*?(\d+)\s*$/) { print '>ID'.$ID++," $number\n"; } else { print; } } =prints >ID1 80 AGGGGGGGGGTTCCC >ID2 85 TTTCCCGGGAAAA >ID3 112 GGCCCCTTTGAGG =cut __DATA__ >Sample_1_x80 AGGGGGGGGGTTCCC >Sample_2_x85 TTTCCCGGGAAAA >sample_3_x112 GGCCCCTTTGAGG
Well, if you want to get the sample number from the ">" line then:
while (<DATA>) { if (my ($sample, $number) = $_ =~ /^>.*?(\d+).*?(\d+)\s*$/) { print '>ID'.$sample," $number\n"; } else { print; } }
which will print the same thing

Replies are listed 'Best First'.
Re^2: modifying a file with regex!
by rna_follower (Initiate) on Mar 17, 2012 at 00:15 UTC
    Thanks everyone for your useful comments/codes!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://960080]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2018-04-23 15:01 GMT
Find Nodes?
    Voting Booth?