shadedecho
Programmer
So, the basic concept here is that I have some text output that comes from another command (which one matters not here)... so it might have something like:
blah192.168.0.2blahblah
on a single line output'd to the screen. using the regexp
[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}
I want to have the output from a single command line (piped together in whatever way necessary) be JUST the matching IP address.
so, something like
is where i would need to put something in which would actually display the matched string. sed's documentation states that & would do the trick, BUT, the sed command above actually prints the rest of the line too, which I want to discard.
So, I have two thoughts on how to do this:
1. find a way to tell the regular expression to match the negation, in other words, everything that does NOT match the above regular expressions which find the IP. Then, everything that didn't match (actually, matched the negation of the expression) would be replaced by empty string. This would leave only the IP address.
2. find a way using & in the replace and also how to make a new line character in there, so it would print the match on one line and the rest that didn't match on another line. My thinking with this is that this could then be piped to grep and grep would only return the line with the match'd sequence again.
Maybe there's another way to do this. I can't seem to make either of the above approaches work. Please, can someone help me write a single command line using grep, awk, and/or sed that will take a single line of any kind of text and extract JUST a single IP address from that line?
blah192.168.0.2blahblah
on a single line output'd to the screen. using the regexp
[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}
I want to have the output from a single command line (piped together in whatever way necessary) be JUST the matching IP address.
so, something like
Code:
$ ./myscript | sed -e "s/[0-9]\{1,3\}\.[0-9]
\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}//"
^
right here
So, I have two thoughts on how to do this:
1. find a way to tell the regular expression to match the negation, in other words, everything that does NOT match the above regular expressions which find the IP. Then, everything that didn't match (actually, matched the negation of the expression) would be replaced by empty string. This would leave only the IP address.
2. find a way using & in the replace and also how to make a new line character in there, so it would print the match on one line and the rest that didn't match on another line. My thinking with this is that this could then be piped to grep and grep would only return the line with the match'd sequence again.
Maybe there's another way to do this. I can't seem to make either of the above approaches work. Please, can someone help me write a single command line using grep, awk, and/or sed that will take a single line of any kind of text and extract JUST a single IP address from that line?