Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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
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?
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 drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2015-07-28 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (254 votes), past polls