Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.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
Regex 递归地遍历bash+;使用厕所_Regex_Bash_Recursion_Find_Wc - Fatal编程技术网

Regex 递归地遍历bash+;使用厕所

Regex 递归地遍历bash+;使用厕所,regex,bash,recursion,find,wc,Regex,Bash,Recursion,Find,Wc,我需要递归地遍历目录。第一个参数必须是我需要从中开始的目录,第二个参数是描述文件名的regex 例如,/myscript.sh目录“regex” 当脚本递归地遍历目录和文件时,它必须使用wc-l来计算regex描述的文件中的行数 我如何使用find with-exec来实现这一点?或者有其他的方法吗?请帮忙 谢谢是的,您可以使用查找: $ find DIR -iname "regex" -type f -exec wc -l '{}' \; 或者,如果要计算所有文件中的总行数: $ find

我需要递归地遍历目录。第一个参数必须是我需要从中开始的目录,第二个参数是描述文件名的regex

例如,
/myscript.sh目录“regex”

当脚本递归地遍历目录和文件时,它必须使用wc-l来计算regex描述的文件中的行数

我如何使用find with-exec来实现这一点?或者有其他的方法吗?请帮忙


谢谢

是的,您可以使用
查找

$ find DIR -iname "regex" -type f -exec wc -l '{}' \; 
或者,如果要计算所有文件中的总行数:

$ find DIR -iname "regex" -type f -exec wc -l '{}' \; | awk '{ SUM += $1 } END { print SUM }'
您的脚本将如下所示:

#!/bin/bash

# $1 - name of the directory - first argument
# $2 - regex - second argument

if [ $# -lt 2 ]; then
  echo Usage: ./myscript.sh DIR "REGEX"
  exit
fi

find "$1" -iname "$2" -type f -exec wc -l '{}' \;

编辑:-如果需要更高级的正则表达式,请使用
-regextype posix extended
-regex
而不是@sudo_O所指出的
-iname

-iname不支持
regexp
它在脚本中使用
shell globbing
/bin/bash if[“$#”-ne“2”];然后echo“参数数目错误!”echo“$0”退出fi echo$0 echo$1 echo$2查找$1-regextype posix extended-regex'$2'-type f-exec wc-l'{}\;它应该检查作为参数传递的第一个目录中包含的子文件夹。对不起,我是新来的。。这样就可以了,但是,脚本还必须检查作为argument@user2219046-
find
将查看子文件夹。@kamituel您误解了选项
-iname
根本没有使用
regexp
,您需要使用
-regex
<代码>-name和
-iname
与shell globbing一起使用。