douxiong3245 2018-09-11 21:34
浏览 199

Shell脚本或命令删除整个目录中的起始字符和结束字符之间的所有文本

Looking for an easy way to remove all of my TODO: Comments throughout an entire directory of scripts.

If I have:

/*
TODO: Some text here
Some more Here
Some more Here
*/

I want to delete the /* Everything in Between and */ Not a huge deal, just wondering for decreasing file sizes

  • 写回答

1条回答 默认 最新

  • douchun1948 2018-09-12 00:07
    关注

    Here is something to start with:

    $file = 'somefile.php';
    
    $f = fopen($file,'r');
    $temp = dirname($file).'/temp.php';
    print_r($temp);
    $t = fopen($temp, 'w');
    
    $mode = 'write';
    $buffer = [];
    while($line=fgets($f)){
        print_r($mode."
    ");
        switch($mode){
            case 'write':
                if(preg_match('/^\s*\/\\*/', $line)){
                    $buffer[] = $line;
                    $mode = 'comment';
                }else{
                    fputs($t,$line);
                }
            break;
            case 'comment': 
                if(preg_match('/^\s*\\**\s*@TODO/i', $line)){
                    $buffer = [];
                    $mode = 'skip';
                }else if(preg_match('/^\s*\\*\//', $line)){
                    fwrite($t,implode("", $buffer));
                    $buffer = [];
                    fputs($t,$line);
                    $mode = 'write';
                }else{
                    $buffer[] = $line;
                }
            break;
            case 'skip':
                if(preg_match('/^\s*\\*\//', $line)){
                    $mode = 'write';
                }
            break;
        }
    }
    /*
    fclose($t);
    fclose($f);
    rename($temp, $file);
    */
    

    I tested it against this file

    <?php
    $var= "foo";
    
    /*
     @todo hello
     some other shuff
     */
    
    
    
    /**
     * 
     * @author Me
     *
     */
    class someclass{
    
    
    
    }
    

    This was the stuff in temp.php

    <?php
    $var= "foo";
    
    
    
    
    /**
     * 
     * @author Me
     *
     */
    class someclass{
    
    
    
    }
    

    hope it helps. I would probably back up your files before attempting this.

    UPDATE

    Had to fix a couple minor things so it would skip comments without @todo. The rest you can probably work out fairly easily.

    The regex is only there to account for spaces or comments that are like this

    /*
    * @todo
    */
    

    This \\* becomes \* for the regex engine that then becomes a literal *.

    So this one \s*\/\\*

    • \s* - none or more spaces
    • \/ - the / literally
    • \\* - the * literally

    Or in English the /* open comment tag.

    I mention this because the regex is tricky, but you probably have spaces and other stuff in there, probably every @todo isn't the very first thing on the line.

    Doing it like this, you get to handle each line on it's own, it simplifies things and more importantly increases the matching accuracy.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c