Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

grep for numbers

by sandy1028 (Sexton)
on Mar 31, 2009 at 05:46 UTC ( #754329=perlquestion: print w/ replies, xml ) Need Help??
sandy1028 has asked for the wisdom of the Perl Monks concerning the following question:

When I grep for 'ci=' I got this line and also many other line which has 'ci=' But I have to grep for the line where ci= numbers. for eg: ci=4365345 Can you please tell me how to extract ci=numbers and also remove all the characters before ci and all characters after numbers of ci.
<body onunload="params='s=o&n=o&ci=5413226&iv=0&pi=34&sessid=c1e7be2e5 +d3518213a67577cf5b2b1c4bd0a0619&uuid=c1e7be2e5d3518213a67577cf5b2b1c4 +bd0a0619&prt=1238476904&pgtp=article&eagi=&t_v=v&">
How to grep only for

Comment on grep for numbers
Download Code
Re: grep for numbers
by moritz (Cardinal) on Mar 31, 2009 at 06:01 UTC
    I recommend reading perlretut, it teaches you the basics about regular expressions that you'll need for this task.
Re: grep for numbers
by syphilis (Canon) on Mar 31, 2009 at 06:18 UTC
    Hi sandy1028,
    Not sure if this helps, but it's very easy to extract the number that follows ci= from the example you provided:
    use warnings; use strict; no warnings 'numeric'; my $str = q(<body onunload="params='s=o&n=o&ci=5413226&iv=0&pi=34&sess +id=c1e7be2e5d3518213a67577cf5b2b1c4bd0a0619&uuid=c1e7be2e5d3518213a67 +577cf5b2b1c4bd0a0619&prt=1238476904&pgtp=article&eagi=&t_v=v&">); my $num = (split /ci=/, $str)[1] + 0; print $num;
    Cheers,
    Rob
      Why not to use this one?
      my $num = $1 if ($str =~ /ci=(\d+)&/);
Re: grep for numbers
by setebos (Beadle) on Mar 31, 2009 at 08:21 UTC
    Something like this (untested) should work:
    $data = '<body ...>'; push @data, $1 while $data =~ /ci=(\d+)/g;
Re: grep for numbers
by bichonfrise74 (Vicar) on Mar 31, 2009 at 18:08 UTC
    Another solution...
    #!/usr/bin/perl use strict; while( <DATA> ) { print "ci=$1" if /ci=(\d+)/; } __DATA__ <body onunload="params='s=o&n=o&ci=5413226&iv=0&pi=34&sessid=c1e7be2e5 +d3518213a67577cf5b2b1c4bd0a0619&uuid=c1e7be2e5d3518213a67577cf5b2b1c4 +bd0a0619&prt=1238476904&pgtp=article&eagi=&t_v=v&">

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2015-07-05 14:50 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 (67 votes), past polls