在文件上grep以找到两个单词之间的字符串

发布于 2025-02-11 05:13:20 字数 505 浏览 1 评论 0原文

我有一个名为varout.txt的文件,其中包含一个文本,如下所示:

Message: unable to locate element

我使用以下命令在单词消息 element 中获取文本:

result = subprocess.run(['grep -oP \'(?<(Message)).*(?= element)\' /home/ubuntu/varout.txt'],shell=True,capture_output=True)
reason = result.stdout
print(reason)

但是我我的输出要低于我的输出:

b' : unable to locate`/n'

预期的输出应该如下,我出错了哪里?

': unable to locate'

I have a file named varout.txt, which contains a text as given below:

Message: unable to locate element

I have used the below command to fetching the text between the word Message and element:

result = subprocess.run(['grep -oP \'(?<(Message)).*(?= element)\' /home/ubuntu/varout.txt'],shell=True,capture_output=True)
reason = result.stdout
print(reason)

But I am getting below as my output:

b' : unable to locate`/n'

Where expected output should be as below, where I am going wrong ??

': unable to locate'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

人疚 2025-02-18 05:13:20

您将输出作为字节字符串获得。

如果您需要将输出作为Unicode字符串获取,请解码字节:

reason = result.stdout.decode('utf-8')

请参阅演示:

import subprocess
result = subprocess.run([r"grep -oP 'Message\s*\K.*?(?=\s*element)' /home/ubuntu/varout.txt"], shell=True, capture_output=True)
print(result.stdout.decode('utf-8'))
## => : unable to locate

我提高了以下方式:

  • Message - 匹配固定的字符串
  • \ s* - 零或更多的whitespaces
  • \ k - 匹配重置运算符,该操作员丢弃到迄今为止匹配的所有文本
  • (?= \ s*element) - 一个正面的lookahead匹配与零或更多的whitespaces和元素 substring匹配的位置。

You get the output as a byte string.

If you need to get the output as a Unicode string, decode the bytes:

reason = result.stdout.decode('utf-8')

See the demo:

import subprocess
result = subprocess.run([r"grep -oP 'Message\s*\K.*?(?=\s*element)' /home/ubuntu/varout.txt"], shell=True, capture_output=True)
print(result.stdout.decode('utf-8'))
## => : unable to locate

I improved the regex a bit as follows:

  • Message - matches a fixed string
  • \s* - zero or more whitespaces
  • \K - match reset operator that discards all text matched so far
  • .*? - any zero or more chars as few as possible
  • (?=\s*element) - a positive lookahead that matches a location that is immediately followed with zero or more whitespaces and an element substring.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文