FAQ
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

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 5 of 8 | next ›
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