4

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.

  • 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

3 Answers3

5

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
Birei
  • 8,124
4

Using the field seperator variable in awk:

echo "ytmti (192.188.2.3) jjggy" | awk -F'[)(]' '{print $2}'                                                                                              
192.188.2.3
jasonwryan
  • 73,126
1

You could also do:

echo 'ytmti (192.188.2.3) jjggy' | tr -dc '0-9.'
mikeserv
  • 58,310
  • 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 '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 (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 '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