Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
shell脚本,用于搜索目录中的模式并输出查找计数数_Shell_Pattern Matching - Fatal编程技术网

shell脚本,用于搜索目录中的模式并输出查找计数数

shell脚本,用于搜索目录中的模式并输出查找计数数,shell,pattern-matching,Shell,Pattern Matching,我需要一个Shell脚本,它接受两个 它必须在其中搜索和删除的主目录 在主目录中搜索所有文件(.c和.h文件)的模式 它必须打印在主目录和每个子目录中找到的图案编号 main dir --> Total pattern found = 5 | sub dir --> 3 | sub dir --> 2 这应该做到: #!/bin/sh grep -rl "$1" . | sed -r 's/\/[^\/]+$//' | sort | uniq

我需要一个Shell脚本,它接受两个

  • 它必须在其中搜索和删除的主目录
  • 在主目录中搜索所有文件(.c和.h文件)的模式
  • 它必须打印在主目录和每个子目录中找到的图案编号

    main dir    --> Total pattern found = 5
      |
      sub dir   --> 3
      |
      sub dir   --> 2
    
    这应该做到:

    #!/bin/sh
    
    grep -rl "$1" . | sed -r 's/\/[^\/]+$//' | sort | uniq -c
    
    其工作原理如下:

    • 首先使用
      grep
      查找参数
      $1
      中提供的模式
      • -r
        搜索当前目录中的所有文件
        和所有子目录
      • -l
        仅输出文本与模式匹配的文件的
        路径/文件名
    • 从所有输出中删除
      文件名
      ,因此只保留
      路径
      (即目录)
    • 然后对路径进行排序和计数
    其输出将如下所示:

          2 .
          3 ./sub_dir1
          1 ./sub_dir2
          6 ./sub_dir2/a_sub_sub_dir
    

    第一列是目录中模式的匹配数量,第二列是目录的名称(
    是主目录)。

    一种计数方法是使用
    wc
    命令;e、 g.如果您可以使搜索每行生成一个文件,
    wc-l
    打印行数(因此也打印文件数)。您好,这对我很有效,但在shell脚本中,我将此命令作为一个单独的文件,但如何将模式输入此文件