LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 11: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, 12:16 PM   #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, 12:16 PM   #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, 12:22 PM   #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, 12:25 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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, 01:22 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
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, 08:18 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
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, 01: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, 01:40 AM   #9
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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, 01: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, 02:03 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Did you read the previous reply?
 
Old 07-27-2012, 02: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, 02:19 AM   #13
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,825
Blog Entries: 1

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
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, 07:13 AM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 11:44 PM
extract substring from string in C baddah Programming 6 02-02-2010 05:22 AM
Extract certain parts of a string Dynadrate Linux - Software 1 04-05-2009 11:35 PM
Extract value from string ewingtux Programming 1 12-27-2008 08:19 PM
Extract part of a string steven.c.banks Linux - General 7 05-07-2008 08:18 AM

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

All times are GMT -5. The time now is 02:18 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration