msgReader

This parses a newline-delimited HL7 or a SMAT .msg file into segments. This includes an argument (-w <#>) to set line wrapping.

This splits the messages into segments. As alone this makes messages more readable, when combined with grep or other UNIX tools, message analysis becomes much more powerful.

#!/usr/bin/perl -lw
###########################################################################
#
# Name: msgReader
# Purpose: Parse HL7 into segments
#
###########################################################################

use Getopt::Std;
use Text::Wrap;

getopt('w');

if ($opt_w) {
    $Text::Wrap::columns = $opt_w;
}

$/ = "\r";
$i = 0;

while (<>) {
    s/\n//g;
    if (/^MSH/) {
        $i++;
        print "\nMessage: $i\n";
    }
    if ($opt_w) {
        print wrap("", "    ", $_);
    } else {
        print;
    }
}