[英]Sed: Extracting regex pattern from lines
I have an input stream of many lines which look like this: 我有许多行的输入流,如下所示:
path/to/file: example: 'extract_me.proto'
path/to/other-file: example: 'me_too.proto'
path/to/something/else: example: 'and_me_2.proto'
...
I'd like to just extract the *.proto
filenames from these lines, and I have tried: 我只想从这些行中提取*.proto
文件名,我已经尝试过:
[INPUT] | sed 's/^.*\([a-zA-Z0-9_]+\.proto\).*$/\1/'
I know that part of my problem is that .*
is greedy and I'm going to get things like e.proto
and o.proto
and 2.proto
, but I can't even get that far... it just outputs with the same lines as the input. 我知道我的问题的一部分是.*
是贪婪的,我将得到e.proto
和o.proto
和2.proto
,但我什至不能走那么远...它只是输出与输入相同的行。 Any help would be greatly appreciated. 任何帮助将不胜感激。
I find it helpful to use extended regex for this purpose ( -r
) in which case you need not escape your brackets. 我发现为此目的使用扩展的正则表达式( -r
)很有帮助,在这种情况下,您不必逃脱括号。
sed -r 's/^.*[^a-zA-Z0-9_]([a-zA-Z0-9_]+\.proto).*$/\1/'
The addition of [^a-zA-Z0-9_]
forces the .*
to not be greedy. 添加[^a-zA-Z0-9_]
强制.*
不要贪婪。
one way to do it: 一种方法:
sed 's/^.*[^a-zA-Z0-9_]\([a-zA-Z0-9_]\+\.proto\).*$/\1/'
+
char 转义了+
char another way: use single quote delimitation, after all it's here for that: 另一种方式:使用单引号定界,毕竟是在这里:
sed "s/^.*'\([a-zA-Z0-9_]\+\.proto\)'.*\$/\1/"
Use this sed
: 使用此sed
:
sed "s/^.*'\([a-zA-Z0-9_]\+\.proto\).*$/\1/"
+
- Extended-RegEx. +
-扩展正则表达式。 So, you need to escape to get special meaning. 因此,您需要逃脱以获得特殊含义。 The preceding item will be matched one or more times.
Another way: 其他方式:
sed "s/^.*'\([^']\+\.proto\)'.*$/\1/"
使用GNU sed:
sed -E "s/.*'([^']+)'$/\1/"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.