csachinc has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i am trying to extract numerical string from a bunch of combination of words.
for example
Change 1836432 on 2011/07/14 by xyz@xyz 'generation for ' Change 1834312 on 2011/07/14 by xyz@xyz 'merge with 118846 '
i want to extract only
1836432 for first row
, 1834312 from second row.
i tried doing this using split but that not efficient way to do. please suggest some one liner or 2 liner code to do for above rows. Thanks

Replies are listed 'Best First'.
Re: extract numerical string from a bunch of words
by toolic (Bishop) on Jul 19, 2011 at 00:19 UTC
    Read through the documentation for regular expressions, and give it a try:
Re: extract numerical string from a bunch of words
by davido (Cardinal) on Jul 19, 2011 at 08:09 UTC

    There are several ways to approach this, depending on what parts of your input are unlikely to change (ie, what you can reliably anchor off of). For example, is it always the first numeric field per line? Is the numeric field always seven digits? Does it always come after 'Change'? Does it always come before 'on' followed by a date?

    Let's say it's always the first numeric field following the word "Change ". You could match like this:

    my @numbers; # Assuming we're reading from a file line by line... while( <> ) { chomp; next unless length $_; if( m/^Change\s(\d+)\s/ ) { push @numbers, $1; } } print "$_\n" for @numbers;

    But really, I think we would need to know about your input format before we could be sure we're giving you a good answer. Do you slurp it all in at once? Do you read it line by line? ...lots of possibilities that would change the answer in various ways.

    Update: Oh, you want a one-liner. Didn't see that before.

    perl -ne "m/^Change\s(\d+)\s/ && print $1, qq/\n/;"

    Dave

Re: extract numerical string from a bunch of words
by Anonymous Monk on Jul 19, 2011 at 00:42 UTC
Re: extract numerical string from a bunch of words
by planetscape (Chancellor) on Jul 19, 2011 at 20:57 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.