perl as command line

perl as command line

am 24.05.2011 06:11:45 von vishesh kumar

--bcaec5014c153e01ef04a3fdc926
Content-Type: text/plain; charset=ISO-8859-1

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,

--

--bcaec5014c153e01ef04a3fdc926--

Re: perl as command line

am 24.05.2011 08:29:45 von Jim Gibson

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/'


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: perl as command line

am 24.05.2011 09:59:04 von Shlomi Fish

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 .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: perl as command line

am 24.05.2011 13:43:33 von vishesh kumar

--bcaec548a361013bb404a40419e4
Content-Type: text/plain; charset=ISO-8859-1

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

--bcaec548a361013bb404a40419e4--

Re: perl as command line

am 27.05.2011 06:58:20 von vishesh kumar

--bcaec548a40d65148704a43ac98c
Content-Type: text/plain; charset=ISO-8859-1

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

--bcaec548a40d65148704a43ac98c--

Re: perl as command line

am 27.05.2011 07:15:49 von Jim Gibson

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.


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: perl as command line

am 27.05.2011 07:54:12 von vishesh kumar

--bcaec50161512bbb4504a43b91bb
Content-Type: text/plain; charset=ISO-8859-1

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

--bcaec50161512bbb4504a43b91bb--

Re: perl as command line

am 27.05.2011 14:26:14 von Ireneusz Pluta

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

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/