douli2063 2015-05-21 21:18
浏览 48
已采纳

Bash循环按文件计算php标签

I would like to have a bash script that counts how many occurrences of to find orphaned tags. I think it would be something like

The command I would use to count occurrences is grep -o "

Maybe

for i in *.php; do
    open = grep -o "<?php" $i | wc -l
    close = grep -o "?>" $i | wc -l
    echo $i open close
done

file1.php 5 5
file2.php 4 5

Also can it be converted into a one line command?

  • 写回答

2条回答 默认 最新

  • douxinghuai3150 2015-05-22 16:12
    关注

    Any time you write a loop in shell just to manipulate text you have the wrong approach. In this case the script you had and the answer you selected will both be immensely inefficient and fragile and will produce bizarre unexpected output and/or syntax errors depending on the contents of the directory you run them in.

    The UNIX tool for manipulating text is awk - just use it (in this case I'm using GNU awk for ENDFILE):

    $ cat good.php
    <html>
     <head>
      <title>PHP Test</title>
     </head>
     <body>
     <?php echo '<p>Hello World</p>'; ?>
     <?php echo '<p>Goodbye Cruel World</p>'; ?>
     </body>
    </html>
    

    .

    $ cat bad.php
    <html>
     <head>
      <title>PHP Test</title>
     </head>
     <body>
     <?php echo '<p>Hello World</p>';
     <?php echo '<p>Goodbye Cruel World</p>'; ?>
     <?php echo '<p>Hello Again World</p>'; ?>
     echo '<p>Goodbye Again Cruel World</p>'; ?>
     </body>
    </html>
    

    .

    awk '/<\?php/{++beg} /\?>/{++end} ENDFILE{print FILENAME, beg, end; beg=end=0}' *.php
    bad.php 3 3
    good.php 2 2
    

    The above uses 1 process total for all files instead of 4 per file for your shell script so it will be orders of magnitude more efficient and it will work for ANY file names, including those that contain white space or even newlines.

    Note that, just like your shell loop would behave, the above does not actually detect the mismatches in the bad.php file since there's one missing open and 1 missing close. Fortunately it's also easy to enhance to, say, tell you the line numbers and contents where the open/close lines are mismatching and report them as they occur:

    $ awk '
    FNR==1 { beg=end=0 }
    /<\?php/ {
        if (beg++ > end) {
            print "Warning:", FILENAME, "missing close for the open at line", begFnr, begRec
            beg--
        }
        begFnr = FNR
        begRec = $0
    }
    /\?>/ {
        if (++end > beg) {
            print "Warning:", FILENAME, "missing open for the close at line", FNR, $0
            end--
        }
    }
    ENDFILE {
        if (beg > end) {
            print "Warning:", FILENAME, "missing close for the open at line", begFnr, begRec
        }
    }
    ' *.php
    Warning: bad.php missing close for the open at line 6  <?php echo '<p>Hello World</p>';
    Warning: bad.php missing open for the close at line 9  echo '<p>Goodbye Again Cruel World</p>'; ?>
    

    BTW I use beg/end as the var names above instead of open/close because close() is an awk function name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改