FAQ
Hi Members,

I am a linux system admin. I want to use perl as a command line like sed
and awk.
For example suppose , i need to extract IP Addr from a string or file using
regrex
i mean
str="hello ip is 192.168.2.1 and data is xxx"
And i want ip addr only using Regex
echo $str | perl -pe ??????

Please guide me,

--

Search Discussions

  • Jim Gibson at May 24, 2011 at 6:29 am

    At 9:41 AM +0530 5/24/11, vishesh kumar wrote:
    Hi Members,

    I am a linux system admin. I want to use perl as a command line like sed
    and awk.
    For example suppose , i need to extract IP Addr from a string or file using
    regrex
    i mean
    str="hello ip is 192.168.2.1 and data is xxx"
    And i want ip addr only using Regex
    echo $str | perl -pe ??????
    Try this:

    echo $str | perl -pe 's/[^\d.]//g'

    If your line has other numbers, this will get the first contiguous set:

    echo $str | perl -pe 's/.*?([\d.]+).*/$1/'

    This will make sure there is at least one dot in the substring extracted:

    echo $str | perl -pe 's/.*?(\d+\.[\d.]+).*/$1/'
  • Shlomi Fish at May 24, 2011 at 7:59 am
    Hi Vishesh,
    On Tuesday 24 May 2011 07:11:45 vishesh kumar wrote:
    Hi Members,

    I am a linux system admin. I want to use perl as a command line like sed
    and awk.
    For example suppose , i need to extract IP Addr from a string or file using
    regrex
    i mean
    str="hello ip is 192.168.2.1 and data is xxx"
    And i want ip addr only using Regex
    echo $str | perl -pe ??????
    Please see:

    http://search.cpan.org/dist/Regexp-Common/

    namely http://search.cpan.org/perldoc?Regexp::Common::net .

    Regards,

    Shlomi Fish

    --
    -----------------------------------------------------------------
    Shlomi Fish http://www.shlomifish.org/
    First stop for Perl beginners - http://perl-begin.org/

    "I'm not straight - I'm Israeli."

    Please reply to list if it's a mailing list post - http://shlom.in/reply .
  • Vishesh kumar at May 24, 2011 at 11:44 am
    Thanks Jim
    Your suggestion working great !!!

    On Tue, May 24, 2011 at 11:59 AM, Jim Gibson wrote:

    echo $str | perl -pe 's/.*?(\d+\.[\d.]+).*/$1/'



    --
    http://linuxmantra.com
  • Vishesh kumar at May 27, 2011 at 4:58 am
    Hi Jim

    echo $str | perl -pe 's/.*?(\d+\.[\d.]+).*/$1/'

    Giving desired result, but i wonder what is use of ? in this expression

    On Tue, May 24, 2011 at 11:59 AM, Jim Gibson wrote:
    At 9:41 AM +0530 5/24/11, vishesh kumar wrote:

    Hi Members,

    I am a linux system admin. I want to use perl as a command line like sed
    and awk.
    For example suppose , i need to extract IP Addr from a string or file
    using
    regrex
    i mean
    str="hello ip is 192.168.2.1 and data is xxx"
    And i want ip addr only using Regex
    echo $str | perl -pe ??????
    Try this:

    echo $str | perl -pe 's/[^\d.]//g'

    If your line has other numbers, this will get the first contiguous set:

    echo $str | perl -pe 's/.*?([\d.]+).*/$1/'

    This will make sure there is at least one dot in the substring extracted:

    echo $str | perl -pe 's/.*?(\d+\.[\d.]+).*/$1/'

    --
    http://linuxmantra.com
  • Jim Gibson at May 27, 2011 at 5:15 am

    At 10:28 AM +0530 5/27/11, vishesh kumar wrote:
    Hi Jim
    You should address all of your questions to the list as a whole. That
    way you will get smarter people than me helping you.
    echo $str | perl -pe 's/.*?(\d+\.[\d.]+).*/$1/'

    Giving desired result, but i wonder what is use of ? in this expression
    Did you try it without the question mark? If you do, you will see the
    affect of "greedy" quantifiers. Without the question mark, the first
    '.*' matches the longest string. This means that you won't get the
    full IP address, just the minimal part at its rear end that matches
    the '\d+\.[\d.]+' part, which could be '1.1' for example. With the
    '?', the initial '.*' matches only the part of the string before the
    first digit, and the whole IP address is matched and captured.
  • Vishesh kumar at May 27, 2011 at 5:54 am
    Thanks Jim
    Now i understood completely. Very good explanation.

    On Fri, May 27, 2011 at 10:45 AM, Jim Gibson wrote:
    At 10:28 AM +0530 5/27/11, vishesh kumar wrote:

    Hi Jim
    You should address all of your questions to the list as a whole. That way
    you will get smarter people than me helping you.


    echo $str | perl -pe 's/.*?(\d+\.[\d.]+).*/$1/'
    Giving desired result, but i wonder what is use of ? in this expression
    Did you try it without the question mark? If you do, you will see the
    affect of "greedy" quantifiers. Without the question mark, the first '.*'
    matches the longest string. This means that you won't get the full IP
    address, just the minimal part at its rear end that matches the
    '\d+\.[\d.]+' part, which could be '1.1' for example. With the '?', the
    initial '.*' matches only the part of the string before the first digit, and
    the whole IP address is matched and captured.

    --
    http://linuxmantra.com
  • Ireneusz Pluta at May 27, 2011 at 12:26 pm

    W dniu 2011-05-24 06:11, vishesh kumar pisze:
    Hi Members,

    I am a linux system admin. I want to use perl as a command line like sed
    and awk.
    generally, you might be interested in http://minimalperl.com/.
    HTH
    Irek

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMay 24, '11 at 4:11a
activeMay 27, '11 at 12:26p
posts8
users4
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase