如何使用GREP使用正面的LookBehind和LookAhead从字符串中提取单词?

发布于 2025-02-14 00:41:01 字数 739 浏览 2 评论 0原文

我有一个字符串存储在变量中。

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 技术交流群。

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

发布评论

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

评论(3

请叫√我孤独 2025-02-21 00:41:01

您可以使用此sed解决方案:

fullString="Line 1 Line 2 captcha_decode userEnteredCaptcha poting Line 3"
sed -E 's/.* captcha_decode userEnteredCaptcha ([^ ]+).*/\1/' <<< "$fullString"

poting

或此gnu-grep解决方案也将有效:

grep -oP 'captcha_decode userEnteredCaptcha \K\S+' <<< "$fullString"

poting

You may use this sed solution:

fullString="Line 1 Line 2 captcha_decode userEnteredCaptcha poting Line 3"
sed -E 's/.* captcha_decode userEnteredCaptcha ([^ ]+).*/\1/' <<< "$fullString"

poting

Or this gnu-grep solution would also work:

grep -oP 'captcha_decode userEnteredCaptcha \K\S+' <<< "$fullString"

poting
无法回应 2025-02-21 00:41:01

第一个解决方案: 在您显示的样本中,请尝试以下awk代码。在gnu awk中编写和测试。

echo "$fullString" | 
awk -v RS='captcha_decode userEnteredCaptcha [^ ]*' '
RT{
  num=split(RT,arr,FS)
  print arr[num]
}'


第二解决方案: 使用awk's gnu在gnu awk'的数组中函数在Regex中找到的匹配选项,用于捕获组。

echo "$fullString" | 
awk 'match($0,/captcha_decode userEnteredCaptcha ([^ ]*)/,arr){print arr[1]}'

1st solution: With your shown samples, please try following awk code. Written and tested in GNU awk.

echo "$fullString" | 
awk -v RS='captcha_decode userEnteredCaptcha [^ ]*' '
RT{
  num=split(RT,arr,FS)
  print arr[num]
}'


2nd solution: Using awk's match function in GNU awk's array option for matches found in regex for capturing group.

echo "$fullString" | 
awk 'match($0,/captcha_decode userEnteredCaptcha ([^ ]*)/,arr){print arr[1]}'
一个人的夜不怕黑 2025-02-21 00:41:01
  mawk ++ nf ofs = fs ='^。 。*
poting
gawk'$ _ = $ 2'fs ='^。 。*
mawk ++NF OFS= FS='^.*captcha_decode userEnteredCaptcha | .*
poting
gawk '$_ = $2' FS='^.*captcha_decode userEnteredCaptcha | .*
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文