排除文件的grep数组参数 [英] grep array parameter of excluded files
问题描述
我想从我的 grep
命令中排除一些文件,为此我使用了参数:
- exclude = excluded_file.ext
阅读我想用排除文件的bash数组:
EXCLUDED_FILES =(excluded_file.ext)
然后将$ {EXCLUDED_FILES}数组传递给 grep
,如:
grep -Rnimy text--exclude = $ {EXCLUDED_FILES}
如何将数组作为参数传递给 grep
命令?
grep -Rni$ {excluded_files [@] /#/ - exclude =}my text
参数扩展$ {excluded_files [@] /#/ - exclude =}
将扩展到数组的扩展 excluded_files
,每个字段的前缀为 - exclude =
。看:
$ excluded_files =('* .txt''* .stuff')
$ printf'%s \\\
'$ {excluded_files [@] /#/ - exclude =}
--exclude = *。txt
--exclude = *。stuff
I want to exclude some files from my grep
command, for this I'm using parameter:
--exclude=excluded_file.ext
To make more easy to read I want to use a bash array with excluded files:
EXCLUDED_FILES=("excluded_file.ext")
Then pass ${EXCLUDED_FILES} array to grep
, like:
grep -Rni "my text" --exclude=${EXCLUDED_FILES}
How I can pass an array as parameter to grep
command?
I guess you're looking for this:
grep -Rni "${excluded_files[@]/#/--exclude=}" "my text"
The parameter expansion "${excluded_files[@]/#/--exclude=}"
will expand to the expansion of the array excluded_files
with each field prefixed with --exclude=
. Look:
$ excluded_files=( '*.txt' '*.stuff' )
$ printf '%s\n' "${excluded_files[@]/#/--exclude=}"
--exclude=*.txt
--exclude=*.stuff
这篇关于排除文件的grep数组参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!