开发者

Using non-consuming matches in Linux find regex

Here's my problem in a simplified scenario.

Create some test files:

touch /tmp/test.xml    
touch /tmp/excludeme.xml    
touch /tmp/test.ini    
touch /tmp/test.log   

I have a find expression that returns me all the XML and INI files:

[root@myserver] ~> find /tmp -name -prune -o -regex '.*\.\(xml\|ini\)'    
/tmp/test.ini    
/tmp/test.xml    
/tmp/excludeme.xml    

I now want a way of modifying this -regex to exclude the excludeme.xml file from being included in the results.

I thought this should be possible by using/combining a non-consuming regex (?=expr) with a negated match (?!expr). Unfortunately I can't quite get the format of the command right, so my attempts result in no matches being returned. Here was one of my attempts (I've tried many different forms of this with different escaping!):

find /tmp -name -prune -o -regex '\(?=.*exclu开发者_如何学运维deme\.xml\).*\.\(xml\|ini\)'   

I can't break down the command into multiple steps (e.g. piping through grep -v) as the find command is assumed as input into other parts of our tool.


This does what you want on linux:

find /tmp -name -prune -o -regex '.*\.\(xml\|ini\)' \! -regex '.*excludeme\.xml'

I'm not sure if the "!" operator is unique to gnu find.


Not sure about what escapes you need or if lookarounds work, but these work for Perl:

/^(?!.*\/excludeme\.).*\.(xml|ini)$/

/(?<!\/excludeme)\.(xml|ini)$/

Edit - Just checked find command, best you can do with find is to change the regextype to -regextype posix-extended but that doesen't do stuff like look-arounds. The only way around this looks to be using some gnu stuff, either as @unholygeek suggests with find or piping find into gnu grep with the -P perl option. You can use the above regex verbatim if you go with a gnu grep. Something like find .... -print | xargs grep -P ...

Sorry, thats the best I can do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜