duanjing9339
2017-11-10 10:07
浏览 174
已采纳

sed正则表达式切割字符串

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?

图片转代码服务由CSDN问答提供 功能建议

我有大约200个php文件,其中包含一些 iconv()函数, somelike like 这样的:

 <代码>的iconv( 'GB2312', 'UTF-8',$ AROW [$ aColumns [3]])
iconv( 'GB2312', 'UTF-8'  ,$ rs1 ['供应商']); 
iconv('GB2312','UTF-8',$ aRow [$ aColumns [$ i]]); 
   
 
 我不知道什么是在批处理模式下删除 iconv('GB2312','UTF-8',和最终的最佳方法,而不触及 变量。 
 
 

此RegEx可以匹配我的情况,但我不知道如何使用 sed ^(iconv \(\ 'GB2312 \'\,\'UTF-8 \'\,)+ |(\))

我也不确定 sed 在这种情况下是正确的解决方案

之前遇到类似问题的人?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dregvw1801 2017-11-10 10:19
    已采纳

    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)

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongwaner1367 2017-11-10 10:22

    Try this cut statement with the output.

    cat file|cut -d, -f3|tr -d ');'
    
    评论
    解决 无用
    打赏 举报
  • duangai1916 2017-11-10 11:05

    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.

    评论
    解决 无用
    打赏 举报
  • douweihui0178 2017-11-10 11:53

    Try this sed:

    cat file | sed 's/iconv(.*,.*,\(.*\)).*/\1/g'
    

    This will extract only the variable.

    评论
    解决 无用
    打赏 举报
  • 「已注销」 2017-11-10 16:35

    With grep

    grep -o '$[^)]*' infile
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题