0

I've got a test result log to parse that is basically just a bunch of key=value pairs on sequential lines separated by a // at the end.

Everything seems great except the output file. In normal Notepad, it looks fine. In Notepad++, it has a bunch of extra lines. When viewing symbols from the output file, original file lines end in CR and wholly blank new lines are injected as CRLF.

It's input file looks the same in both, and all lines end in CRLF. I would like its output file to look the same in both.

What do I need to fix for that desired behavior? I tried adding a gsub to strip away lines that were blank after the first replace, contents = contents.gsub(/^\n/m, "") but that doesn't seem to change the output.

Input File, test_results.log:

// 
Test_App=moduleTwo_test.exe 
Test_Function=genericTestOne() 
Test_Iterations=5 
Test_Health=100 %  
Test_Result=PASSED   
Test_Path=C:\rb-tests\moduleTwo_test.exe 
// 
Test_App=subModuleOne_test.exe 
Test_Function=iAlwaysFail() 
Test_Iterations=5 
Test_Failures=5     
Test_Health=0 %  
Test_Result=FAILED   
Retest_Iterations=20 
Retest_Failures=20 
Retest_Health=0 %  
Retest_Result=FAILED   
Test_Path=C:\rb-tests\subModuleOne_test.exe 
// 
Test_App=subModuleOne_test.exe 
Test_Function=genericTestOne() 
Test_Iterations=5 
Test_Health=100 %  
Test_Result=PASSED   
Test_Path=C:\rb-tests\subModuleOne_test.exe 
//

Ruby Script, find_fails.rb

this_dir = File.dirname(ARGV[0])
this_file = File.open(ARGV[0], "rb")
contents = this_file.read
contents = contents.gsub(/\/\/[ ](?>(?:(?!\/\/).))*?PASSED.*?(?=\/\/)/m, "")
contents = contents.gsub(/^\n/m, "")
File.write(this_dir + "\\fails.log", contents)

Output File, fails.log, as seen in Notepad++

// 

Test_App=subModuleOne_test.exe 

Test_Function=iAlwaysFail() 

Test_Iterations=5 

Test_Failures=5     

Test_Health=0 %  

Test_Result=FAILED   

Retest_Iterations=20 

Retest_Failures=20 

Retest_Health=0 %  

Retest_Result=FAILED   

Test_Path=C:\rb-tests\subModuleOne_test.exe 

//

Desired Output file/format for fails.log, and what it already looks like in normal Notepad:

// 
Test_App=subModuleOne_test.exe 
Test_Function=iAlwaysFail() 
Test_Iterations=5 
Test_Failures=5     
Test_Health=0 %  
Test_Result=FAILED   
Retest_Iterations=20 
Retest_Failures=20 
Retest_Health=0 %  
Retest_Result=FAILED   
Test_Path=C:\rb-tests\subModuleOne_test.exe 
//
kayleeFrye_onDeck
  • 6,648
  • 5
  • 69
  • 80
  • 1
    In Notepad++ use menu => **View** => **Show symbols** => **Show end of line** (or even **Show all symbols**) to show the actual characters present. Then you will know what to change. It is possible that the file is encoded with a unusual mix of end-of-line characters. – AdrianHHH Mar 29 '17 at 05:52
  • It was! For some bizarre reason, `gsub` was replacing all the `CRLF`s with `CR`s. I didn't figure out a way to get it working with one `gsub`, but I added a second line, `contents = contents.gsub(/\r$/m, "")` that finds the lines ending in `CR`, nukes them, and seems to swap them out with a `CRLF` without me specifying it in the replace field. The original file has all `CRLF`s, so if you or someone else can figure out a way to update the first `gsub` to handle the replace in one operation instead of two, I would consider that an acceptable answer. – kayleeFrye_onDeck Mar 29 '17 at 18:34
  • @AdrianHHH it looks like this is a common enough problem with either Ruby in-general or specifically gsub substitutions when it comes to linebreaks. The last quantifier should be also replacing the linebreak, but seems to be either adding or not removing the carriage return. I'd have to do some more testing to hammer it out. Feel free to convert your comment to an answer. – kayleeFrye_onDeck Mar 31 '17 at 21:28

0 Answers0