排除文件的grep数组参数 [英] grep array parameter of excluded files

查看:80
本文介绍了排除文件的grep数组参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆