Search code examples
phpregexbashsediconv

sed regex cut string


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?


Solution

  • 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)