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 |
Quote:
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 |
clunky I know but
cat source_file | sed {s/"ip=ip_address "//} | sed {s/'"'//g} |
alright here is another way too
awk {'print $2'} sourcefile | sed {s/'"'//g} |
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:
|
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' |
Assuming the simple case
Code:
echo "ip=ip_address 172.16.0.17"|cut -d' ' -f2 |
i want extracted data to be stored into another variable
|
Hi,
Quote:
Code:
y=$(some command) |
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 |
Did you read the previous reply?
|
@grail
your solutions just prints the value 172.16.0.17, it doesn't insert in into another variable... |
Quote:
|
Quote:
... 451239798... ..567. |
All times are GMT -5. The time now is 08:02 AM. |