php - sed regex with replace -


given file:

<?  $dbo = mysql_fetch_assoc();  $dbo->employee_id;  $dbo->employee_name;  $dbo->dirt;  $dbo->dirt23;  $dbo->dirt_2_3;  ?> 

and using sed find , replace:

sed -e "s|$dbo->([\w\_]*)|$dbo['\1']|g" test.php 

it returns error. i've tried using apostrophes container , escaping , using forward slash command delimiter, no avail.

result should be:

<?  $dbo = mysql_fetch_assoc();  $dbo['employee_id'];  $dbo['employee_name'];  $dbo['dirt'];  $dbo['dirt23'];  $dbo['dirt_2_3'];  ?> 

help, please.

few mistakes:

  1. \w includes underscore
  2. $ needs excaped
  3. square brackets need excaped
  4. you're using $ inside double quotes while expanded shell

use following sed:

sed -r 's/(\$dbo->)([[:alnum:]_]+)(.*)$/\1["\2"]\3/g' 

or on osx:

sed -e 's/(\$dbo->)([[:alnum:]_]+)(.*)$/\1["\2"]\3/g' 

for using single quotes:

sed -r "\$s/(dbo->)([a-za-z0-9_]+)(.*)$/\1['\2']\3/g" 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -