Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I am just starting out using sed and I intend to use it to extract IP addresses from ping output. Here's what I am trying to achieve exactly:

input text:

ytmti ( jjggy

desired output:

command I am trying:

echo "ytmti ( jjggy" | sed 's:\((\(\))\):\2:' 

current output:

ytmti jjggy

Note: ytmti and jjggy are really stand-ins for text like Pinging unix.stackexchange.com and with 32 bytes of data:.

I think using awk might be a better solution for parsing ping output, but I would like to get accustomed to sed.

share|improve this question

marked as duplicate by cuonglm, slm, rahmu, Patrick, Caleb Jun 24 '14 at 15:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Please note that Note:...etc above was inserted by others and does not accurately reflect the question as it was first asked. Whether it accurately reflects op's original intent i cannot say, but it was certainly added after the question was asked. Please see this meta question about it. – mikeserv Jun 24 '14 at 17:45
up vote 5 down vote accepted

The substitution command of sed replaces all characters matched in first section with all character of second section, so you will need .* or similar and group only the part to save, like:

echo " ytmti ( jjggy" | sed 's:^.*(\([^)]*\).*$:\1:'

Note that I use [^)]* that avoids to hardcode the IP and generalize it for any of them. It yields:
share|improve this answer

Using the field seperator variable in awk:

echo "ytmti ( jjggy" | awk -F'[)(]' '{print $2}'                                                                                     
share|improve this answer

You could also do:

echo 'ytmti ( jjggy' | tr -dc '0-9.'
share|improve this answer
Read the question: “I … intend to use [sed] to extract IP addresses from ping output.” The output from ping commonly begins with a line something like Pinging unix.stackexchange.com ( with 32 bytes of data:. Your answer includes in the output (a) the 32, (b) the .s in the domain name, and (c) any numerals that happen to be in the domain name. – Scott Jun 24 '14 at 16:19
@Scott - I did read the question, though I am afraid you may not have. I think you must have missed the part of the question wherein op says: I am trying: echo " ytmti ( jjggy". Still, it matters not. This was never intended as a practical example for how to render any data into an ip address, but merely as an example of what one or another tool might do with a certain example set. And so you are free to believe whatever nonsense makes you most comfortable. – mikeserv Jun 24 '14 at 16:27
@mikeserv while I definitely appreciate your effort, the other answers are much more useful overall since they take into account the fact that I will eventually want to extract an IP from ping. – user293496 Jun 25 '14 at 14:26
@user293496 we're agreed. It wasn't intended to be very practical - just to give you or anyone else an idea that there are always several ways to work through a sample set. I didn't really expect the one upvote it's got... – mikeserv Jun 25 '14 at 16:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.