LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to extract from a string (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-extract-from-a-string-4175418806/)

Owais.Ahmad 07-26-2012 10:58 AM

how to extract from a string
 
Hi,

I have a string "ip=ip_address 172.16.0.17", how can i extract 172.16.0.17 from it???

Regards,

Owais

dmdeb 07-26-2012 11:16 AM

Quote:

Originally Posted by Owais.Ahmad (Post 4738678)
Hi,

I have a string "ip=ip_address 172.16.0.17", how can i extract 172.16.0.17 from it???

Regards,

Owais

Hi,

this should do the job:

echo "ip=ip_address 172.16.0.17" | grep -wo "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"

Regards
dmdeb

Sydney 07-26-2012 11:16 AM

clunky I know but
cat source_file | sed {s/"ip=ip_address "//} | sed {s/'"'//g}

Sydney 07-26-2012 11:22 AM

alright here is another way too
awk {'print $2'} sourcefile | sed {s/'"'//g}

pixellany 07-26-2012 11:25 AM

It's important to focus on the rule to be followed----in this case, you might be wanting several different things:
---that exact IP address
---ANY legal IP address
---Anything following the word "ip_address"

If it helps, here's an excerpt from another thread---with a Regex that matches any legal IP:
Quote:

Regex to find old-style (dotted quad) IPs
Code:

([0-9]{1,3}\.){3}[0-9]{1,3}
This uses extended regex rules, so use with egrep, sed -r, etc.

grail 07-26-2012 12:22 PM

I am not sure if 'ip' is a variable and so we work with that or if the whole string is being used. I will assume the latter as the solution is easily adaptable to 'ip':
Code:

x='ip=ip_address 172.16.0.17'
echo ${x##* }


chrism01 07-26-2012 07:18 PM

Assuming the simple case
Code:

echo "ip=ip_address 172.16.0.17"|cut -d' ' -f2

Owais.Ahmad 07-27-2012 12:37 AM

i want extracted data to be stored into another variable

evo2 07-27-2012 12:40 AM

Hi,

Quote:

Originally Posted by Owais.Ahmad (Post 4739120)
i want extracted data to be stored into another variable

In the following, the output of "some command" will be stored in the variable y.
Code:

y=$(some command)
Evo2.

Owais.Ahmad 07-27-2012 12:48 AM

i'm using the following command :

echo "ip=$ip" | grep -wo "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"

this command extracts the data but i want to save it into another variable

grail 07-27-2012 01:03 AM

Did you read the previous reply?

Owais.Ahmad 07-27-2012 01:10 AM

@grail

your solutions just prints the value 172.16.0.17, it doesn't insert in into another variable...

sycamorex 07-27-2012 01:19 AM

Quote:

Originally Posted by Owais.Ahmad (Post 4739142)
@grail

your solutions just prints the value 172.16.0.17, it doesn't insert in into another variable...

grail was referring to post #9.

pixellany 07-27-2012 06:13 AM

Quote:

Originally Posted by Owais.Ahmad (Post 4739130)
i'm using the following command :

echo "ip=$ip" | grep -wo "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"

this command extracts the data but i want to save it into another variable

That will not reliably extract IP addresses.....It will, for example match these strings:
...
451239798...
..567.


All times are GMT -5. The time now is 08:02 AM.