bash 如何在unix shell脚本中循环匹配正则表达式的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36743957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to loop through files that match a regular expression in a unix shell script
提问by paul frith
I want to be able to loop through a list of files that match a particular pattern. I can get unix to list these files using ls and egrep with a regular expression, but I cannot find a way to turn this into an iterative process. I suspect that using ls is not the answer. Any help would be gratefully received.
我希望能够遍历匹配特定模式的文件列表。我可以让 unix 使用带有正则表达式的 ls 和 egrep 列出这些文件,但我找不到将其转换为迭代过程的方法。我怀疑使用 ls 不是答案。任何帮助将不胜感激。
My current ls command looks as follows:
我当前的 ls 命令如下所示:
ls | egrep -i 'MYFILE[0-9][0-9]([0][1-9]|1[0-2])([0][1-9]|[12][0-9]|[3][01]).dat'
I would expect the above to match:
我希望以上内容匹配:
- MYFILE160418.dat
- myFILE170312.DAT
- MyFiLe160416.DaT
- MYFILE160418.dat
- myFILE170312.DAT
- 我的文件160416.DaT
but not:
但不是:
- MYOTHERFILE150202.DAT
- Myfile.dat
- myfile.csv
- MYOTHERFILE150202.DAT
- 我的文件
- 我的文件.csv
Thanks,
谢谢,
Paul.
保罗。
采纳答案by 123
You can use (GNU) find
with the regex search option instead of parsing ls
.
您可以将 (GNU)find
与正则表达式搜索选项一起使用,而不是解析ls
.
find . -regextype "egrep" \
-iregex '.*/MYFILE[0-9][0-9]([0][1-9]|1[0-2])([0][1-9]|[12][0-9]|[3][01]).dat' \
-exec [[whatever you want to do]] {} \;
Where [[whatever you want to do]]
is the command you want to perform on the names of the files.
[[whatever you want to do]]
您要对文件名执行的命令在哪里。
From the man page
从手册页
-regextype type Changes the regular expression syntax understood by -regex and -iregex tests which occur later on the command line. Currently-implemented types are emacs (this is the default),posix-awk, posix-basic, posix-egrep and posix-extended. -regex pattern File name matches regular expression pattern. This is a match on the whole path, not a search. For example, to match a file named `./fubar3', you can use the regular expression `.*bar.' or `.*b.*3', but not `f.*r3'. The regular expressions understood by find are by default Emacs Regular Expressions, but this can be changed with the -regextype option. -iregex pattern Like -regex, but the match is case insensitive.
-regextype type Changes the regular expression syntax understood by -regex and -iregex tests which occur later on the command line. Currently-implemented types are emacs (this is the default),posix-awk, posix-basic, posix-egrep and posix-extended. -regex pattern File name matches regular expression pattern. This is a match on the whole path, not a search. For example, to match a file named `./fubar3', you can use the regular expression `.*bar.' or `.*b.*3', but not `f.*r3'. The regular expressions understood by find are by default Emacs Regular Expressions, but this can be changed with the -regextype option. -iregex pattern Like -regex, but the match is case insensitive.
回答by paul frith
Based on the link Andy K provided I have used the following to loop based on my matching criteria:
基于 Andy K 提供的链接,我使用以下内容根据我的匹配标准进行循环:
for i in $(ls | egrep -i 'MYFILE[0-9][0-9]([0][1-9]|1[0-2])([0][1-9]|[12][0-9]|[3][01]).dat' ); do
echo item: $i;
done