I have about 200 php files that containing some iconv()
functions, something like this:
iconv('GB2312','UTF-8',$aRow[$aColumns[3]])
iconv('GB2312','UTF-8',$rs1['supplier']);
iconv('GB2312','UTF-8',$aRow[ $aColumns[$i] ]);
i don't know what could be the best way to remove iconv('GB2312','UTF-8',
and final )
in batch mode without touching the variable.
this RegEx could match my case but i don't know how to use it with sed
: ^(iconv\(\'GB2312\'\,\'UTF-8\'\,)+|(\))
And i am also not sure that sed
is the right solution in this case
Anyone faced a similar problem before?
You can use this sed
command:
sed -i "s/iconv('GB2312','UTF-8',\([^)]*\))\(.*\)/\1\2/" file
which will extract your php variable into \1
. /2
is the remaining of the line (a ;
in the example you posted)
Try this cut
statement with the output.
cat file|cut -d, -f3|tr -d ');'
This might work for you (GNU sed):
sed 's/iconv('\''GB2312'\'','\''UTF-8'\'',\([^)]*\))/\1/g' file
Use an example string as a template, replacing '
by '\''
and the variable to be kept as any characters from after the second ,
which is not a closing )
. This variable is enclosed in quoted (...)
i.e. \(...\)
which may be transfered to the RHS of the substitution command as a back reference.
Try this sed:
cat file | sed 's/iconv(.*,.*,\(.*\)).*/\1/g'
This will extract only the variable.
With grep
grep -o '$[^)]*' infile