匹配可选模式与查找命令

发布于 2025-02-11 10:15:49 字数 373 浏览 2 评论 0原文

我想将所有文件与典型的fortran文件扩展名与查找匹配。典型的扩展是.f .f .for .f90 .f90 .f90 .f95 .f95 .f95 .f03 .f03 .f03 .f08 .f08 .f08 .f15 .f15 .f15 .f15 .f18 .f18

我到目前为止尝试的是find * -Name“ * -Name” * * \。[ff] [0-9OO] [0-9rr]“,尽管.f and .f和.f。为此,我必须制作[0-9OO]和[0-9rr]可选的,通常用“?”完成。但是,此查找 * -name“ * \。[ff] [0-9oo]?[0-9rr]?”不适合我的find版本。

I want to match all files with a typical fortran file extension with find. Typical extensions are .f .F .for .FOR .f90 .F90 .f95 .F95 .f03 .F03 .f08 .F08 .f15 .F15 .f18 .F18

what I tried so far is find * -name "*\.[Ff][0-9Oo][0-9Rr]" this works for all the mentioned extensions despite .f and .F. For that I have to make [0-9Oo] and [0-9Rr] optional which is usually done with "?". However, this find * -name "*\.[Ff][0-9Oo]?[0-9Rr]?" does not work with my version of find.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅听莫相离 2025-02-18 10:15:49

使用gnu 查找,您可以使用

find * -regextype posix-extended -regex '.*\.[Ff][0-9Oo]?[0-9Rr]?'

该模式匹配完全匹配的字符串:

  • *\。 - 任何文本到最右边的。包括它
  • [FF] - ff
  • [0-9OO]? - 可选数字,oo
  • [0-9rr]? - 可选数字,rr

您还可以使用仅仅环球模式使用非regex方法:

find * ( -name "*.[Ff][0-9oO][0-9rR]" -o -name "*.[Ff]" )

请注意,* Glob模式与任何文本匹配。在上一个解决方案中,它是一个量词,是一个使前面模式匹配零或更多次的操作员。另外,在Glog模式中,匹配字面的点,无需逃脱它。

With a GNU find, you can use

find * -regextype posix-extended -regex '.*\.[Ff][0-9Oo]?[0-9Rr]?'

The pattern matches a string that fully matches:

  • .*\. - any text up to the rightmost . including it
  • [Ff] - F or f
  • [0-9Oo]? - an optional digit, O or o
  • [0-9Rr]? - an optional digit, R or r.

You can also use a non-regex approach using mere glob patterns:

find * ( -name "*.[Ff][0-9oO][0-9rR]" -o -name "*.[Ff]" )

Note that here, the * glob pattern matches any text. In the previous solution, it was a quantifier, an operator that makes the preceding pattern match zero or more times. Also, in glob patterns, . matches a literal dot, no need to escape it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文