Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 (192.188.2.3) jjggy

desired output:

192.188.2.3

command I am trying:

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

current output:

ytmti 192.188.2.3 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 Gnouc, slm, rahmu, Patrick, Caleb Jun 24 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 at 17:45

3 Answers 3

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 (192.188.2.3) jjggy" | sed 's:^.*(\([^)]*\).*$:\1:'

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

192.188.2.3
share|improve this answer

Using the field seperator variable in awk:

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

You could also do:

echo 'ytmti (192.188.2.3) 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 (198.252.206.140) 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 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 (192.188.2.3) 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 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 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 at 16:55

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