print multi-lines in a text file matching the start string & end string

print multi-lines in a text file matching the start string & end string

Post by powa » Tue, 05 May 2009 23:54:59


If there are some lines in a text file matching the start string
"Test:" and end string "TEST FAILED **" then print them and also the
strings containing "java", "Total:", "Passed:" and "Failed:".
How to do that using awk or sed or ...?
e.g.
Input is:
powah@pc2 xml_test$ java -jar xmltest.jar Testing/Encryption.xml
Test: 1 - Encrypt Data AESKEY
### ENCRYPTS SOME DATA AESKEY###

Type: Expected Value Test
Results:
Tag <dss:Result> contains:
'<dss:ResultMajor>urn:oasis:names:tc:dss:resultmajor:RequestorError></
dss:ResultMajor><dss:ResultMessage>Could not find specified key
(AESKEY)></dss:ResultMessage>'
Stored data to: encryptedData1
** TEST FAILED **
Test: 2 - Inject Key
### TESTS INJECTING A KEY ###

Type: Expected Value Test
Results:
Tag <dss:Result> contains:
'<dss:ResultMajor>urn:oasis:names:tc:dss:resultmajor:Success></
dss:ResultMajor>'

** TEST PASSED **
Test: 3 - Encrypt Data DESKEY
### ENCRYPTS SOME DATA DESKEY###

Type: Expected Value Test
Results:
Tag <dss:Result> contains:
'<dss:ResultMajor>urn:oasis:names:tc:dss:resultmajor:RequestorError></
dss:ResultMajor><dss:ResultMessage>Could not find specified key
(DESKEY)></dss:ResultMessage>'
Stored data to: encryptedData1
** TEST FAILED **
====================
SUMMARY

Total: 3
Passed: 1
Failed: 2
====================

*** All Tests have completed. ***


powah@pc2 xml_test$ java -jar xmltest.jar Testing/Decryption.xml
Test: 1 - Decrypt Data AESKEY
### DECRYPTS SOME DATA AESKEY###

Type: Expected Value Test
Results:
Tag <dss:Result> contains:
'<dss:ResultMajor>urn:oasis:names:tc:dss:resultmajor:Success></
dss:ResultMajor>'

** TEST PASSED **
====================
SUMMARY

Total: 1
Passed: 1
Failed: 0
====================

*** All Tests have completed. ***



Output will be:
powah@pc2 xml_test$ java -jar xmltest.jar Testing/Encryption.xml
Test: 1 - Encrypt Data AESKEY
### ENCRYPTS SOME DATA AESKEY###

Type: Expected Value Test
Results:
Tag <dss:Result> contains:
'<dss:ResultMajor>urn:oasis:names:tc:dss:resultmajor:RequestorError></
dss:ResultMajor><dss:ResultMessage>Could not find specified key
(AESKEY)></dss:ResultMessage>'
Stored data to: encryptedData1
** TEST FAILED **
Test: 3 - Encrypt Data DESKEY
### ENCRYPTS SOME DATA DESKEY###

Type: Expected Value Test
Results:
Tag <dss:Result> contains:
'<dss:ResultMajor>urn:oasis:names:tc:dss:resultmajor:RequestorError></
dss:ResultMajor><dss:ResultMessage>Could not find specified key
(DESKEY)></dss:ResultMessage>'
Stored data to: encryptedData1
** TEST FAILED **
Total: 3
Passed: 1
Failed: 2
 
 
 

print multi-lines in a text file matching the start string & end string

Post by Hai V » Wed, 06 May 2009 02:01:15

The following should more or less do what you are looking for. You can
then tweak it to your precise specifications. Save the following block
of code in a file call summary.awk:
#
# Start Block
#
/java|Total:|Passed:|Failed:/ { print; }
/^Test/ { save = 1; lineCount = 0 }
save == 1 { line[lineCount++] = $0 }
/TEST FAILED/ { save = 0; for (i = 0; i < lineCount; i++) { print line
[i] } }
#
# End Block
#

Assume that your data is in a file call 'data.txt', issue the
following command:
awk -f summary.awk data.txt

Good luck.
Hai

 
 
 

print multi-lines in a text file matching the start string & end string

Post by Hai V » Wed, 06 May 2009 04:30:15


Jon,
The /^Test/ pattern/action should take care of that scenario. I have
tried my solution on the original data and verified that was the case.
Please let me know if I miseed anything.
Hai
 
 
 

print multi-lines in a text file matching the start string & end string

Post by powa » Wed, 06 May 2009 23:14:57


I want to print "java", "Total:", "Passed:" and "Failed:" only if the
test had failed.
I modify your summary.awk as follows, it works but seems verbose, is
there a shorter way?
#
# Start Block
#
/java/ { javacmd = $0 }
/^Test/ { save = 1; lineCount = 0 }
save == 1 { line[lineCount++] = $0 }
/TEST FAILED/ {
print javacmd;
save = 0;
failed = 1;
for (i = 0; i < lineCount; i++) { print line [i] }
}
/Total:|Passed:/ { if (failed) print; }
/Failed:/ { if (failed) {failed = 0; print; } }
#
# End Block
#
 
 
 

print multi-lines in a text file matching the start string & end string

Post by Hai V » Thu, 07 May 2009 04:05:18


> I want to print "java", "Total:", Passed:" and "Failed:" only if the >> test had failed. >> I modify your summary.awk as follows, it works but seems verbose, is >> there a shorter way? >> # >> # Start Block >> # >> /java/ { javacmd = $0 } >> /^Test/ { save = 1; lineCount = 0 } >> save == 1 { line[lineCount++] = $0 } >> /TEST FAILED/ { >> rint javacmd;> > ave = 0>
> ailed = >;
> or (i = <; i < lineCount; i++) { print line [i]>}}> >
> /Total:|Passed:/ { if (failed) print> }
> /Failed:/ { if (failed) {failed = 0; print; > }
> # End Bl>ck
> #
If it works then keep it. We can probably do better, but it it works,
it works.
Hai