Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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

Replies are listed 'Best First'.
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 (Chancellor) 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;
      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://754329]
Approved by moritz
and the radiator hisses contentedly...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2017-04-30 09:40 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (535 votes). Check out past polls.