LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-26-2012, 10:58 AM   #1
Owais.Ahmad
LQ Newbie
 
Registered: Jul 2012
Posts: 10

Rep: Reputation: Disabled
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
 
Old 07-26-2012, 11:16 AM   #2
dmdeb
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Debian
Posts: 45

Rep: Reputation: 6
Quote:
Originally Posted by Owais.Ahmad View Post
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
 
1 members found this post helpful.
Old 07-26-2012, 11:16 AM   #3
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
clunky I know but
cat source_file | sed {s/"ip=ip_address "//} | sed {s/'"'//g}
 
Old 07-26-2012, 11:22 AM   #4
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
alright here is another way too
awk {'print $2'} sourcefile | sed {s/'"'//g}
 
Old 07-26-2012, 11:25 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 07-26-2012, 12:22 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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##* }
 
1 members found this post helpful.
Old 07-26-2012, 07:18 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Assuming the simple case
Code:
echo "ip=ip_address 172.16.0.17"|cut -d' ' -f2
 
1 members found this post helpful.
Old 07-27-2012, 12:37 AM   #8
Owais.Ahmad
LQ Newbie
 
Registered: Jul 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
i want extracted data to be stored into another variable
 
Old 07-27-2012, 12:40 AM   #9
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

Quote:
Originally Posted by Owais.Ahmad View Post
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.
 
Old 07-27-2012, 12:48 AM   #10
Owais.Ahmad
LQ Newbie
 
Registered: Jul 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-27-2012, 01:03 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Did you read the previous reply?
 
Old 07-27-2012, 01:10 AM   #12
Owais.Ahmad
LQ Newbie
 
Registered: Jul 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
@grail

your solutions just prints the value 172.16.0.17, it doesn't insert in into another variable...
 
Old 07-27-2012, 01:19 AM   #13
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by Owais.Ahmad View Post
@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.
 
Old 07-27-2012, 06:13 AM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by Owais.Ahmad View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
extract a string within a string using a pattern adshocker Linux - Newbie 1 11-04-2010 10:44 PM
extract substring from string in C baddah Programming 6 02-02-2010 04:22 AM
Extract certain parts of a string Dynadrate Linux - Software 1 04-05-2009 10:35 PM
Extract value from string ewingtux Programming 1 12-27-2008 07:19 PM
Extract part of a string steven.c.banks Linux - General 7 05-07-2008 07:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration