1

Alright, So i'm confused & out of ideas.

I'm trying to replace all the old IP addresses with a new one.

find -type f | grep \.php$ | xargs sed -r -i "s/(\'|\")(localhost|127.0.0.1|10.0.32.4|10.0.32.5)(\'|\")/\"10.0.32.165\"/g"

I'm trying to find, all the PHP files, and replace any of the following (localhost, 127.0.0.1, 10.0.32.4, 10.0.32.5) with 10.0.32.165.

find * | grep \.php$ | xargs egrep "(\'|\")(localhost|127.0.0.1|10.0.32.4|10.0.32.5)(\'|\")"

After the first command is run, I want to make sure there's no traces of old values left, so i run the above command, but it unfortunately returns

siteeval/application/libraries/fetcher.url.class.php:    $this->host = "10.0.32.4";

Anyone see what i'm doing wrong? or maybe can clue me into a better way of doing this?

grufftech
  • 6,760
  • 4
  • 37
  • 37

3 Answers3

2

Try this:

sed -e 's/\(127\.0\.0\.1\|10\.0\.32\.4\|localhost\|10\.0\.32\.5\)/10.0.32.165/g'

You need to escape the parenthesis and the pipes. If you want to make sure they are always between quotes, try:

sed -e 's/\"\(127\.0\.0\.1\|10\.0\.32\.4\|localhost\|10\.0\.32\.5\)\"/10.0.32.165/g'

Testing it a bit:

$ echo '"10.0.32.4"' | sed -e 's/\"\(127\.0\.0\.1\|10\.0\.32\.4\|localhost\|10\.0\.32\.5\)\"/10.0.32.165/g'
10.0.32.165
$ echo '"localhost"' | sed -e 's/\"\(127\.0\.0\.1\|10\.0\.32\.4\|localhost\|10\.0\.32\.5\)\"/10.0.32.165/g'
10.0.32.165

A suggestion for the future. Always try first with small samples (using echo like this), before moving to the complicated case.

*Edit: Escaped the dots too.

sucuri
  • 2,867
  • 1
  • 23
  • 22
1

1) You have to escape periods, a period matches any character. So to have it match a literal period, you need to escape it by putting a back slash before it:

\.

It will just seem to work because the meta-character period still matches a period.

2) Do you really need to match only IPs that are in quotes? If not that you don't have to worry about matching them and putting them back, that just makes it even more of a mess.

3) Any reason you can't simplify this by running the command multiple times for each IP?

4) Matching IP address with regex leads to insanity, and possibly convulsions, consider something like Perl's Net::IP::Match::Regexp :-)

5) Are you sure there are no carriage returns in that file confusing sed? try cat -vE file and look for ^M

6) Get rid of the grep and just pass -name '*.php' to find instead

Kyle Brandt
  • 83,619
  • 74
  • 305
  • 448
0

I think the only problem you have is that you need to un-escape the single quotes. I like this slightly better, though:

find -type f -name '*.php' | xargs sed -r -i "s/\('|\"\)\(localhost|127.0.0.1|10.0.32.4|10.0.32.5\)\('|\"\)/${1}10.0.32.165${3}/g"

In addition to un-escaping the single quotes, I am saving the matched quote character, so it can be restored around the new IP. Here's the grep:

find . -type f -name '*.php' | xargs egrep "('|\")(localhost|127.0.0.1|10.0.32.4|10.0.32.165)('|\")"
Chad Huneycutt
  • 2,116
  • 1
  • 16
  • 14