Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: modifying a file with regex!

by Marshall (Prior)
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


Comment on Re: modifying a file with regex!
Select or Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2014-08-22 10:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (153 votes), past polls