简体   繁体   English

计算包含数组中不同变量的目录中的文件数 - bash 脚本

[英]Counting the number of files in a directory that contain the different variables in my array - bash script

I have a bash script, which needs to check certain files for certain variables, and count how many files come back containing those variables.我有一个 bash 脚本,它需要检查某些文件中的某些变量,并计算返回包含这些变量的文件的数量。

As there is more than one variable I need to look for I decided to to use an array for the variables.由于需要查找的变量不止一个,因此我决定对变量使用数组。

The code I am using is below:我正在使用的代码如下:

#!/bin/bash

declare -a MYARRAY=('Variable One' 'Variable Two' 'Variable Three');
COUNT_MYARRAY=$(find $DIRECTORY -mtime -1 -exec grep -ln $MYARRAY {} \; | wc -l)

I have declared the $DIRECTORY in my real script.我已经在我的真实脚本中声明了 $DIRECTORY。 However, it does not seem to pick up files if they have the second and third variable within?但是,如果文件中有第二个和第三个变量,它似乎不会拾取文件?

Can anyone see where I might be going wrong?谁能看到我可能出错的地方?

You can use grep s regex support and pass multiple expressions using 'var1\\|var2' .您可以使用grep的正则表达式支持并使用'var1\\|var2'传递多个表达式。 First construct the grep argument and then execute grep.首先构造grep参数,然后执行grep。

You don't need line numbers -n to grep to count the files...您不需要行号-ngrep来计算文件...

grep can handle multiple files - it will be faster to pass multiple files to one grep with -exec ... + , rather then spawn grep for each file. grep可以处理多个文件 - 使用-exec ... +将多个文件传递给一个grep会更快,而不是为每个文件生成grep

UPPER_CASE_VARIABLES are shouting at me and by convention upper vase variables are reserved for exported variables. UPPER_CASE_VARIABLES 对我大喊大叫,按照惯例,上花瓶变量是为导出的变量保留的。

myarray=('Variable One' 'Variable Two' 'Variable Three')
arg=$(printf "%s\|" "${MYARRAY[@]}" | sed 's/\\|$//')
directory=.
count_myarray=$(find "$directory" -type f -mtime -1 -exec grep -l "$arg" {} + | wc -l)

Alternatively: you can pass multiple -exec arguments to find.或者:您可以传递多个-exec参数来查找。 So first from myarray construct arguments to find in the form -exec grep -l <the var> .因此,首先从myarray构造参数以在-exec grep -l <the var>形式中find Note that multiple variables can be in same files, so get unique filenames after grepping.请注意,多个变量可以在同一个文件中,因此在 grepping 后获得唯一的文件名。

myarray=('Variable One' 'Variable Two' 'Variable Three');
findargs=()
for i in "${MYARRAY[@]}"; do
    findargs+=(-exec grep -l "$i" {} +)
done
directory=.
count_myarray=$(find "$directory" -type f -mtime -1 "${findargs[@]}" | sort -u | wc -l)

or similar:或类似:

count_myarray=$(printf '-exec\0grep\0-l\0%s\0{}\0+\0' "${myarray[@]}" | xargs -0 find "$directory" -type f -mtime -1 | sort -u | wc -l)

Remember to quote your variable expansions to protect against whitespaces or special characters in filenames and directory names.请记住引用您的变量扩展以防止文件名和目录名中出现空格或特殊字符。

Going wrong:出错:
With echo $MYARRAY you find Variable One , not the string you want for grep.使用echo $MYARRAY您会找到Variable One ,而不是您想要的 grep 字符串。
Also note that it is better to use lowercase for your variable names.另请注意,变量名最好使用小写。 I will use ${directory} and not $DIRECTORY (and in double quotes for directories with a space).我将使用${directory}而不是$DIRECTORY (对于带有空格的目录,使用双引号)。

You have more options with grep . grep有更多选择。 When you want a file with 8 occurances counted one, you can not use the grep option -c .如果您希望将出现 8 次的文件计为 1,则不能使用grep选项-c An useful option is -r .一个有用的选项是-r You are looking for something like你正在寻找类似的东西

grep -Erl "Variable One|Variable Two|Variable Three" | wc -l

This is difficult when the variables might have special characters like $ or |当变量可能有特殊字符如$|时,这很困难| . .
Another option of grep is using the option -f FILE, Obtain patterns from FILE, one per line grep另一个选项是使用选项-f FILE, Obtain patterns from FILE, one per line

So you should make a function that writes the variables to a file, and use something like所以你应该制作一个将变量写入文件的函数,并使用类似的东西

grep -rlFf "myVariablesFile" "${directory}" | wc -l

When the content of the file is changing rapidly, you might want to avoid the temporary file with当文件内容快速变化时,您可能希望避免临时文件

grep -rlFf <(function_that_writes_variables_to_stdout) "${directory}"| wc -l

or directly或直接

grep -rlFf <(printf "%s\n" "${var1}" "${var2}" "${var3}") "${directory}" | wc -l

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM