Announcement

Collapse
No announcement yet.

How to compare two text files with same title of strings but may with different lines

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to compare two text files with same title of strings but may with different lines

    Dear,

    I'd like to write a script to compare many text files like attached ones for an example(actually there are many text files like:

    sg_logs-sdaa-before.txt
    sg_logs-sdaa-after.txt

    sg_logs-sdab-before.txt
    sg_logs-sdab-after.txt

    sg_logs-sdac-before.txt
    sg_logs-sdac-after.txt
    .....ect.

    And I would like the scrip to auto compare:

    sg_logs-sdxx-before.txt
    sg_logs-sdxx-after.txt

    Plus, the number behind below strings in both text files will be compared:

    attached phy identifier = x
    Invalid DWORD count = x
    Running disparity error count = x
    Loss of DWORD synchronization = x
    Phy reset problem = x
    <where above x means a number>

    As the attached example file, in the sg_logs-sdaa-before.txt we can see above strings presented twice:

    attached phy identifier = 8
    Invalid DWORD count = 259
    Running disparity error count = 244
    Loss of DWORD synchronization = 50
    Phy reset problem = 0

    attached phy identifier = 16
    Invalid DWORD count = 60
    Running disparity error count = 53
    Loss of DWORD synchronization = 14
    Phy reset problem = 0

    And in another attached example text file, there is also contents below strings:

    attached phy identifier = 8
    Invalid DWORD count = 259
    Running disparity error count = 244
    Loss of DWORD synchronization = 52
    Phy reset problem = 0

    attached phy identifier = 16
    Invalid DWORD count = 60
    Running disparity error count = 53
    Loss of DWORD synchronization = 14
    Phy reset problem = 1

    So I would like the script to compare above strings(means to compare the number behind each strings one by one between two files), like below:

    <compare>
    attached phy identifier = 8 (from sg_logs-sdaa-before.txt)
    attached phy identifier = 8 (from sg_logs-sdaa-after.txt)
    Invalid DWORD count = 259 (from sg_logs-sdaa-before.txt)
    Invalid DWORD count = 259 (from sg_logs-sdaa-after.txt)
    Running disparity error count = 244 (from sg_logs-sdaa-before.txt)
    Running disparity error count = 244 (from sg_logs-sdaa-after.txt)
    Loss of DWORD synchronization = 50 (from sg_logs-sdaa-before.txt)
    Loss of DWORD synchronization = 52 (from sg_logs-sdaa-after.txt)
    Phy reset problem = 0 (from sg_logs-sdaa-before.txt)
    Phy reset problem = 0 (from sg_logs-sdaa-after.txt)
    ......ect.

    And the scrip will detect that the number behind string "Loss of DWORD synchronization" between two text files is different, and then export to "report.txt" to highlight the difference with the two file names like:

    Different contents:
    sg_logs-sdaa-before.txt
    sg_logs-sdaa-after.txt
    ...etc.

    It is to let me know which files get different content of below strings to let me save the time to check all files one by one, so may I get help from you to create a simple script for above needed?

    1. Compare multiple files with specific file name
    2. Compare specific strings with might in different lines between two files

    Your help is highly appreciated!

    Thanks,
    Jacky
    Attached Files

  • #2
    Hello,

    To align files of different names, you'll need one of two strategies. I suggest first starting in the graphical interface to get the comparison running as you want, and then we can work back to getting it into script.

    First, we have Alignment Overrides in BC3 Pro. This is in the Folder Compare, Session Settings, Misc tab. The key is to define a rule that contains the different text. In the above examples, one rule might be:
    *-before.txt = *-after.txt
    or
    *-before* = *-after*

    Once the files are aligned left to right, these pairs are compared, and should show the results you are looking for when you view them (double click).
    And, once aligned, you can use the Expand All command, then set the Display Filter to Show Differences, Select All Files, and use the Actions menu -> File Compare Report.

    The file report works on the selection, and can be configured in a variety of ways, such as different layouts, and set to Show Differences to only show different text. Once you have this report, we can work on a script that loads a saved Folder Compare session with a set Alignment Override, then generate the same report.



    The other strategy requires you to use the file report on one pair of files at a time, which you pass in as command line parameters. This would be repeated calls to bcompare.exe "@c:\bcscript.txt" "c:\file1" "c:\file2" "c:\bcreport1.html"

    where the script then generates and saves the report. Then you would repeat, getting a new report for each file pair. This method is a bit clunkier, but doesn't require Alignment Overrides or a saved Folder Compare session.
    Aaron P Scooter Software

    Comment

    Working...
    X