简体   繁体   English

正则表达式排除少数文件但包括其余部分

[英]regex to exclude few files but include rest

I have multilevel directory structure and I want to list all (png,jpeg files), except files, say, sam.png, pam.jpeg etc from any directory 我有多层目录结构,我想列出所有目录中的所有(png,jpeg文件),例如sam.png,pam.jpeg等文件

Ultimately, I want to list all files as 最终,我想将所有文件列出为

a/b/c/d*.jpg (inlude all jpg, but I want to exclude sam.png from a/b/c/d directory)

a/b/c/d*.jpeg

a/b/c/d*.png

a/e/f/k/h/*.png

...and so on ...等等

How do I do that? 我怎么做?

Use following regex - 使用以下regex -

.(jpg|jpeg|gif|png)$(?<!sam.png|pam.jpeg)

This uses negative look behind . 在后面使用了负面的外观

I'm not completly sure of the context of you problem. 我不能完全确定您遇到的问题。

However you can use find . 但是,您可以使用find . to list and files under a directory (recursive). 列出目录下的文件和文件(递归)。

Use egrep to filter the result with a regexp. 使用egrep过滤带有正则表达式的结果。

And then use egrep -v to filter in a negative way (if it doesn't match stays). 然后使用egrep -v以否定的方式进行过滤(如果不匹配保持符)。

So I guess something like this might do the job: 因此,我想像这样的事情可能会起作用:

find . | egrep "*.(jpg|png)$" | egrep -v "*.(sam.png)"

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

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