Search code examples
pythonlambdareadlines

Searching for specific word and returning specific lines


I got help with the code below but need to know how to get it to do it for all matches.

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)

So if I uncomment the 'break' it only shows the first (as stated) (output)

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

and if I comment out, it only shows a match after the first. (output)

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

How can I get it to return all matches in the file (both outputs above)?

diskDetails.txt sample. The file may contain 4-8 sections like the below. I didn't include every line of each section (starting with ID) because each section is 28 lines long. Assume the keywords are in every section. Also each section is separated by a blank line (if that helps).

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)

How to output and save only sections that contain 'Failed' and only include lines that include the keywords of each section?


Solution

  • Change #1: Appending results to the list. Before you were re-assigning on each loop

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

    Change #2: Update our filter logic to loop over result since it is now a list of lists

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

    Change #3: Loop over the results and write to file

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

    Hope this helps!