在文件上grep以找到两个单词之间的字符串
我有一个名为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将输出作为字节字符串获得。
如果您需要将输出作为Unicode字符串获取,请解码字节:
请参阅演示:
我提高了以下方式:
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:
See the demo:
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 anelement
substring.