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:
\w
includes underscore$
needs excaped- square brackets need excaped
- 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
Post a Comment