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