我需要从下面的文件中grep一个表达式:请帮助我想在disk @ until逗号后grep这个单词。
cat out1 boot-device=/pci@400/pci@2/pci@0/pci@e/scsi@/disk@w5000cca025884d5d,0:a rootmirror rootdisk boot-device-index=0
答案 0 :(得分:0)
喜欢这个?它将打印出来" disk @ w5000cca025884d5d"
import re
file_object = open("input", "r")
for line in file_object:
m = re.search('.*disk@(.*),.*', line)
if m:
print(m.group(1))
答案 1 :(得分:0)
可能需要将逗号作为特殊字符转义:
import re
s = 'cat out1 boot-device=/pci@400/pci@2/pci@0/pci@e/scsi@0/disk@w5000cca025884d5d,0:a rootmirror rootdisk boot-device-index=0'
match = re.search(r'disk@(.*)\,', s)
if match:
print match.group(1)
输出:
w5000cca025884d5d