How do I escape a dot / period in bash string match

1

This is my first post, and I am really appreciating all the answers which helped me so far on that website, I hope this is not a redundant post, but I couldn't find something similar here.

I try to split a String which I'll retrieve from a file into separate IPs.
The String would be like:
. something:[12.12.12.12],[13.13.13.13]

I am trying to do a String manipulation as described on tldp
expr "$string" : '\($substring\)'

When trying to escape a dot / period it won't find it:

testString=[12.12.12.12],[13.13.13.13]
echo `expr "$stringZ" : '\(\\[.{1,3}\..{1,3}\..{1,3}\..{1,3}\\]\)'`

I noticed, that I have to double escape [ and I tried to do so with the dot / period but it doesn't help.
How can I achieve that ?

Yes I know this regex is not very good, but I'd like to get the dot / period problem resolved first, before I do more on the regex.

Thanks a lot

Andreas Grimm

Posted 2015-11-03T12:27:29.023

Reputation: 13

What are you trying to do? Store the IPs in an array? – choroba – 2015-11-03T13:00:01.090

Yes, or then as a "space separated" string which i could go through easily with a for loop. – Andreas Grimm – 2015-11-04T14:00:00.907

Answers

0

You can use parameter expansion:

#!/bin/bash
string='. something:[12.12.12.12],[13.13.13.13]'

string=${string##*:}         # Delete up to the last colon.
string=${string//[^0-9.]/ }  # What's not a dot or digit gets replaced by a space.

for ip in $string ; do
    echo "IP: $ip!"
done

choroba

Posted 2015-11-03T12:27:29.023

Reputation: 14 741

works very well. Thanks a lot. Although I still wonder why the regex in my question is not working as I expect it to. Anyhow, again, Thanks a lot. – Andreas Grimm – 2015-11-05T11:36:25.777