Hi,
This solution is a bit less efficient since it now relies on external binaries rather than shell built-ins, but for every block that has a Warning or Error, this will print out the Institution ID and the text of the error or warning.
#!/bin/bash
IFS=''
input=EXTRN071_copy.txt
tmp=/tmp/script.tmp
echo institution,errormessage
while read -r line
do
case "$line" in
*BEGIN\ MESSAGE*)
unset print
echo "$line" > "$tmp"
;;
*END\ MESSAGE*)
echo "$line" >> "$tmp"
if [ "$print" == "1" ]
then
institution=`/usr/bin/awk '$0 ~ / Institution/ {sub(/\r$/,""); print $NF}' "$tmp"`
errormessage=`/bin/grep -E -A2 "^Warning|^Error" "$tmp" | /usr/bin/tail -1`
echo $institution,$errormessage
fi
;;
Warning*|Error*)
print=1
echo "$line" >> "$tmp"
;;
*)
echo "$line" >> "$tmp"
;;
esac
done < "$input"
Sample output:
$ ./script.sh
institution,errormessage
00000029,Original presentment Not Found !
00000029,Non-financial original Slip Not Found !
00000029,Processing Failed For Transaction!
00000046,Transaction type of chargeback is not the same as that of original presentment.
00000046,Transaction type of chargeback is not the same as that of original presentment.
00000041,Original presentment Not Found !
00000041,Non-financial original Slip Not Found !
00000041,Processing Failed For Transaction!
00000041,Original presentment Not Found !
00000041,Non-financial original Slip Not Found !
00000041,Processing Failed For Transaction!
00000050,Original presentment Not Found !
00000050,Non-financial original Slip Not Found !
00000050,Processing Failed For Transaction!
00000050,Original presentment Not Found !
00000050,Non-financial original Slip Not Found !
00000050,Processing Failed For Transaction!
00000007,Original Transaction Not Found !
00000007,Processing Failed For Transaction!
00000007,No transactions processed!
00000007,PROCESSING ERROR! - check log for error messages.
$
Hope this helps in the meantime.
EDIT: If you want the output sorted, change the last line to:
done < "$input" | /usr/bin/sort