简体   繁体   English

Sed:从行中提取正则表达式模式

[英]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.protoo.proto2.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_]强制.*不要贪婪。

Since you tag your command with , I'll assume you have GNU grep. 由于您使用标记了命令,因此我假设您具有GNU grep。 Pick one of 选择其中之一

grep -oP '\w+\.proto' file
grep -o "[^']+\\.proto" file

one way to do it: 一种方法:

sed 's/^.*[^a-zA-Z0-9_]\([a-zA-Z0-9_]\+\.proto\).*$/\1/'
  • escaped the + char 转义了+ char
  • put a negation before the alphanum+underscore to delimit the leading chars 在alphanum +下划线前加一个负号来分隔前导字符

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM