开发者

Safely find files matching pattern and literals

Given a filename like 'prefix.extension', I would like to find all files of the pattern prefix\.\d\d\.extension. Given that 'prefix' or 'extension' could contain literal strings like .*, \n and the like, the only way I can think of finding this properly is to escape all characters in 'prefix' and 'extension', placing them before and after \.\d\d\., and egrep-ing it. Is there a more elegant way of doing t开发者_运维问答his and/or some simple way to escape all special characters for egrep in a Bash script?

Note that putting a backslash in front of every character will change the semantics of some, like \w.


Turns out grep --perl-regexp with \Q and \E does what I need:

ls -1N -- "${source_dir}" | grep -P "^\Q${source_base}.\E\d\{${fragment_digits}\}\Q.${source_extensions}\E\$"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜