Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://960080]
help
Chatterbox?
[Corion]: So Hyperboria is an envrypted, decentralized IPv6 mesh network. I'm interested and want to look how good coverage is in my area. But the API for querying Hyperboria is only reachable over Hyperboria itself. Thanks but no thanks.

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2017-06-23 07:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (536 votes). Check out past polls.