使用 ?与sed [英] Using ? with sed

查看:53
本文介绍了使用 ?与sed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想获取可以gzip压缩或不可以gzip压缩的文件的编号.但是,似乎sed中的正则表达式不支持?.这是我尝试过的:

I just want to get the number of a file that may or may not be gzip'd. However, it appears that a regular expression in sed does not support a ?. Here's what I tried:

echo'file_1.gz'| sed -n's/.*_ \(.* \)\(\.gz \)?/\ 1/p'

,没有任何返回.然后我在要分析的字符串中添加了?:

and nothing was returned. Then I added a ? to the string being analyzed:

echo'file_1.gz?'| sed -n's/.*_ \(.* \)\(\.gz \)?/\ 1/p'

并得到:

1

因此,看起来sed不支持大多数正则表达式中使用的?,对吗?那么,我只想sed为 file_1 file_1.gz 给出一个 1 .如果执行时间很关键,在bash脚本中执行此操作的最佳方法是什么?

So, it looks like the ? used in most regex's is not supported in sed, right? Well then, I would just like sed to give a 1 for file_1 and file_1.gz. What's the best way to do that in a bash script if execution time is critical?

推荐答案

x?等效的是 \(x \ | \).

但是,许多版本的sed支持启用扩展正则表达式"的选项,其中包括?.在GNU sed中,标记为 -r .请注意,这也会更改未转义的括号以进行分组.例如:

However, many versions of sed support an option to enable "extended regular expressions" which includes ?. In GNU sed the flag is -r. Note that this also changes unescaped parens to do grouping. eg:

echo 'file_1.gz'|sed -n -r 's/.*_(.*)(\.gz)?/\1/p'

实际上,您的正则表达式中还有另一个错误,就是如果有一个错误,那么在括号中的贪婪的.* 将会吞噬".gz".据我所知,sed并不具有等同于 * 的非贪婪特性,但是您可以使用 | 来解决此问题.sed中的 | (以及许多其他正则表达式实现)将使用最左边的匹配项,因此您可以执行以下操作:

Actually, there's another bug in your regex which is that the greedy .* in the parens is going to swallow up the ".gz" if there is one. sed doesn't have a non-greedy equivalent to * as far as I know, but you can use | to work around this. | in sed (and many other regex implementations) will use the leftmost match that works, so you can do something like this:

echo 'file_1.gz'|sed -r 's/(.*_(.*)\.gz)|(.*_(.*))/\2\4/'

这将尝试与.gz匹配,并且仅在不起作用时尝试不带.gz的匹配.实际上,第2或第4组中只有一个存在(因为它们位于同一 | 的相对侧),因此我们只需将它们连接起来即可获得所需的值.

This tries to match with .gz, and only tries without it if that doesn't work. Only one of group 2 or 4 will actually exist (since they are on opposite sides of the same |) so we just concatenate them to get the value we want.

这篇关于使用 ?与sed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆