如何使用GREP使用正面的LookBehind和LookAhead从字符串中提取单词?
我有一个字符串存储在变量中。
fullString="Line 1 Line 2 captcha_decode userEnteredCaptcha poting Line 3"
我只想从此字符串中提取POTING
。如果我回荡弦并通过GREP将结果输送为:
echo $fullString | grep "captcha_decode userEnteredCaptcha"
我在Stdout中再次返回整个行。
Line 1 Line 2 captcha_decode userEnteredCaptcha poting Line 3
如果我使用-o选项如下,
echo $fullString | grep -o "captcha_decode userEnteredCaptcha"
我只返回我作为参数提供的部分,
captcha_decode userEnteredCaptcha
我只想提取poting
Captcha_decode userEnteredCaptcha poting
>并由其他字符取得成功第3行
。我如何使用GREP来实现正面的外观和lookahead并仅提取陶艺
?
I have a string literal stored in a variable.
fullString="Line 1 Line 2 captcha_decode userEnteredCaptcha poting Line 3"
I only want to extract the word poting
from this string. If I echo the string and pipe the result through a grep as follows:
echo $fullString | grep "captcha_decode userEnteredCaptcha"
I am returned the entire line again in the stdout.
Line 1 Line 2 captcha_decode userEnteredCaptcha poting Line 3
If I use -o option as following
echo $fullString | grep -o "captcha_decode userEnteredCaptcha"
I am returned only the part I had supplied as argument to the -o option
captcha_decode userEnteredCaptcha
I want to extract only the word poting
which comes after captcha_decode userEnteredCaptcha
and is succeeded by other characters Line 3
. How do I use grep to achieve a positive lookbehind and lookahead and extract only poting
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用此
sed
解决方案:或此
gnu-grep
解决方案也将有效:You may use this
sed
solution:Or this
gnu-grep
solution would also work:第一个解决方案: 在您显示的样本中,请尝试以下
awk
代码。在gnuawk
中编写和测试。第二解决方案: 使用
awk
'sgnu
在gnuawk
'的数组中函数在Regex中找到的匹配选项,用于捕获组。1st solution: With your shown samples, please try following
awk
code. Written and tested in GNUawk
.2nd solution: Using
awk
'smatch
function in GNUawk
's array option for matches found in regex for capturing group.gawk'$ _ = $ 2'fs ='^。 。*
gawk '$_ = $2' FS='^.*captcha_decode userEnteredCaptcha | .*