搜索特定单词并返回特定行

Searching for specific word and returning specific lines

我在下面的代码中得到了帮助,但需要知道如何让它对所有比赛都这样做。

result = []
# 1. Search for the first failed entry:
with open('diskDetails.txt', 'r') as f:
    lines = f.readlines()
    for idx, line in enumerate(lines):
        if ': Failed' in line:
            result = lines[idx-3:idx+25]  # save lines from 'ID' to 'Sub Vendor'
            #break  # only the first match

# 2. Filter result:
keywords = ('ID', 'State', 'Capacity', 'Product ID', 'Serial No.')
result = list(filter(lambda l: str(l).startswith(keywords), result))

# 3. Save result:
with open('dataFinal.txt', 'wt') as f:
    f.writelines(result)

所以如果我取消注释 'break' 它只显示第一个(如前所述) (输出)

ID                              : 0:1:2
State                           : Failed
Capacity                        : 558.38 GB (599550590976 bytes)
Product ID                      : ST3600057SS
Serial No.                      : 6SL7YFWF

如果我注释掉,它只会显示第一个之后的匹配项。 (输出)

ID                              : 0:1:5
State                           : Failed
Capacity                        : 558.38 GB (599550590976 bytes)
Product ID                      : ST3600057SS
Serial No.                      : 6SLAWWHQ

我怎样才能得到它 return 文件中的所有匹配项(上面的两个输出)?

diskDetails.txt 样本。该文件可能包含 4-8 个部分,如下所示。我没有包括每个部分的每一行(从 ID 开始),因为每个部分有 28 行长。假设关键字在每个部分中。此外,每个部分都由一个空行分隔(如果有帮助的话)。

ID                              : 0:1:0
Status                          : Ok
Name                            : Physical Disk 0:1:0
State                           : Online
Power Status                    : Spun Up
Bus Protocol                    : SAS
Media                           : HDD
Part of Cache Pool              : Not Applicable
Remaining Rated Write Endurance : Not Applicable
Failure Predicted               : No
Revision                        : ES66
Driver Version                  : Not Applicable
Model Number                    : Not Applicable
T10 PI Capable                  : No
Certified                       : Yes
Encryption Capable              : No
Encrypted                       : Not Applicable
Progress                        : Not Applicable
Mirror Set ID                   : 0
Capacity                        : 558.38 GB (599550590976 bytes)
Used RAID Disk Space            : 558.38 GB (599550590976 bytes)

ID                              : 0:1:1
Status                          : Ok
Name                            : Physical Disk 0:1:0
State                           : Online
Power Status                    : Spun Up
Bus Protocol                    : SAS
Media                           : HDD
Part of Cache Pool              : Not Applicable
Remaining Rated Write Endurance : Not Applicable
Failure Predicted               : No
Revision                        : ES66
Driver Version                  : Not Applicable
Model Number                    : Not Applicable
T10 PI Capable                  : No
Certified                       : Yes
Encryption Capable              : No
Encrypted                       : Not Applicable
Progress                        : Not Applicable
Mirror Set ID                   : 0
Capacity                        : 558.38 GB (599550590976 bytes)
Used RAID Disk Space            : 558.38 GB (599550590976 bytes)

ID                              : 0:1:2
Status                          : Critical
Name                            : Physical Disk 0:1:2
State                           : Failed
Power Status                    : Spun Up
Bus Protocol                    : SAS
Media                           : HDD
Part of Cache Pool              : Not Applicable
Remaining Rated Write Endurance : Not Applicable
Failure Predicted               : No
Revision                        : ES66
Driver Version                  : Not Applicable
Model Number                    : Not Applicable
T10 PI Capable                  : No
Certified                       : Yes
Encryption Capable              : No
Encrypted                       : Not Applicable
Progress                        : Not Applicable
Mirror Set ID                   : 0
Capacity                        : 558.38 GB (599550590976 bytes)

ID                              : 0:1:3
Status                          : Ok
Name                            : Physical Disk 0:1:3
State                           : Online
Power Status                    : Spun Up
Bus Protocol                    : SAS
Media                           : HDD
Part of Cache Pool              : Not Applicable
Remaining Rated Write Endurance : Not Applicable
Failure Predicted               : No
Revision                        : ES66
Driver Version                  : Not Applicable
Model Number                    : Not Applicable
T10 PI Capable                  : No
Certified                       : Yes
Encryption Capable              : No
Encrypted                       : Not Applicable
Progress                        : Not Applicable
Mirror Set ID                   : 0
Capacity                        : 558.38 GB (599550590976 bytes)
Used RAID Disk Space            : 558.38 GB (599550590976 bytes)

ID                              : 0:1:4
Status                          : Ok
Name                            : Physical Disk 0:1:4
State                           : Online
Power Status                    : Spun Up
Bus Protocol                    : SAS
Media                           : HDD
Part of Cache Pool              : Not Applicable
Remaining Rated Write Endurance : Not Applicable
Failure Predicted               : No
Revision                        : ES64
Driver Version                  : Not Applicable
Model Number                    : Not Applicable
T10 PI Capable                  : No
Certified                       : Yes
Encryption Capable              : No
Encrypted                       : Not Applicable
Progress                        : Not Applicable
Mirror Set ID                   : 0
Capacity                        : 558.38 GB (599550590976 bytes)
Used RAID Disk Space            : 558.38 GB (599550590976 bytes)

ID                              : 0:1:5
Status                          : Non-Critical
Name                            : Physical Disk 0:1:5
State                           : Failed
Power Status                    : Spun Up
Bus Protocol                    : SAS
Media                           : HDD
Part of Cache Pool              : Not Applicable
Remaining Rated Write Endurance : Not Applicable
Failure Predicted               : Yes
Revision                        : ES66
Driver Version                  : Not Applicable
Model Number                    : Not Applicable
T10 PI Capable                  : No
Certified                       : Yes
Encryption Capable              : No
Encrypted                       : Not Applicable
Progress                        : Not Applicable
Mirror Set ID                   : Not Applicable
Capacity                        : 558.38 GB (599550590976 bytes)
Used RAID Disk Space            : 558.38 GB (599550590976 bytes)

ID                              : 0:1:6
Status                          : Ok
Name                            : Physical Disk 0:1:6
State                           : Online
Power Status                    : Spun Up
Bus Protocol                    : SAS
Media                           : HDD
Part of Cache Pool              : Not Applicable
Remaining Rated Write Endurance : Not Applicable
Failure Predicted               : No
Revision                        : ES66
Driver Version                  : Not Applicable
Model Number                    : Not Applicable
T10 PI Capable                  : No
Certified                       : Yes
Encryption Capable              : No
Encrypted                       : Not Applicable
Progress                        : Not Applicable
Mirror Set ID                   : 0
Capacity                        : 558.38 GB (599550590976 bytes)
Used RAID Disk Space            : 558.38 GB (599550590976 bytes)

如何只输出和保存包含'Failed'的部分,并且只包含包含每个部分关键字的行?

更改 #1:将结果附加到列表中。在您重新分配每个循环之前

if ': Failed' in line:
    result.append(lines[idx-3:idx+25])

更改 #2:更新我们的过滤器逻辑以遍历结果,因为它现在是列表的列表

results = list(filter(lambda l: str(l).startswith(keywords), r) for r in result)

更改 #3:遍历结果并写入文件

with open('dataFinal.txt', 'wt') as f:
    for result in results:
        f.writelines(result)

希望对您有所帮助!

您使用行 "result = []" 创建了一个空列表,很好。

您重新定义列表 "result" 每次通过 for 循环时使用以下行: 结果 = 行 [idx-3:idx+25]

您想在 for 循环中添加到列表 "result" 中,内容如下: result.append(行[idx-3:idx+25])