msgParser examples

This splits HL7 messages into fields.

You can use this script on SMAT .msg files and files that are generated from msgExtract or msgReader.Then, you can pipe data to it from different places.

This script correctly counts MSH fields starting with the first pipe (|) as MSH.01. MSH.02 is usually "^~\&". Most of the time the pipe is the field delimiter, so this is hardcoded. You can alter the script to work on messages where there is a different delimiter.

The script left pads a 1-digit number with a zero, so that grep can be more accurate and simpler to use. This lines up the fields on the screen.

In these examples, the first thing is to gather messages from a SMAT .msg file and put them in test.hl7.

msgExtract bnd01ms4_in.tin.0809062315.msg | grep 'TEST\^'
			 > test.hl7

The remainder of the examples operate on this test.hl7 file.

  • msgParser test.hl7
    Message 1
    
    MSH.01: |
    MSH.02: ^~\&
    MSH.03: MS4OC
    MSH.04: 100
    MSH.05: CERNER
    MSH.06: 100
    MSH.07: 20080905060049
    MSH.09: ORR^O02
    MSH.10: 00000000001786872
    MSH.11: T
    MSH.12: 2.3
    MSA.01: AA
    PID.01: 1
    PID.02: 000700228
    PID.03: 444756
    PID.05: TEST^MCKESSON^^^
    PID.07: 19451004
    PID.08: F
    PID.10: W
    PID.11: 9000 PHARMACY ROAD^^LANDLOW^MO^65214^^^
    PID.16: M
    PID.17: UNK
    PID.18: 00260049580
    PID.19: 158963111
    ORC.01: NA
    ORC.02: 00010
    ORC.03: 7789399237789399
    ORC.05: IP
    ORC.06: N
    ORC.07: 1^ONCE^^200809050600^^9
    ORC.09: 200809050600
    ORC.10: 350987^MAR
    ORC.12: 00428^PETERS^WALTER^R^^^MD
    ORC.13:   4000
    OBR.02: 00010
    OBR.03: 7789399237789399
    OBR.04: 0337345^POTASSIUM^^K^^
    OBR.15: ^^^^^
    OBR.16: 00428^PETERS^WALTER^R^^^MD
    OBR.24: 05000000
    OBR.27: 1^ONCE^^200809050600^^9
    OBR.39: ^^^^^
    ZOD.01: 01030649620001100033734523778939923778939900000000101
    
    Message 2
    
    MSH.01: |
    MSH.02: ^~\&
    MSH.03: MS4OC
    MSH.04: 100
    MSH.05: CERNER
    ...
  • msgParser test.hl7 | egrep '^$|Message|MSH.09|PID.05'
     This shows blank lines: Message #, MSH.09, and PID.05.
    Message 1
    
    MSH.09: ORR^O02
    PID.05: TEST^MCKESSON^^^
    
    Message 2
    
    MSH.09: ORR^O02
    PID.05: TEST^TERESA^X^^
    
    Message 3
    
    MSH.09: ADT^A28
    PID.05: HORIZON^TEST^^^
    
    Message 4
    
    MSH.09: ADT^A31
    PID.05: HORIZON^TEST^^^
    
    Message 5
    
    MSH.09: ADT^A05
    PID.05: HORIZON^TEST^^^
    
    Message 6
    
    MSH.09: ADT^A08
    PID.05: HORIZON^TEST^^^
    
    Message 7
    
    MSH.09: ADT^A01
    PID.05: HORIZON^TEST^^^
    
    Message 8
    
    MSH.09: ADT^A08
    PID.05: HORIZON^TEST^^^
    
    Message 9
    
    MSH.09: ORR^O02
    PID.05: TEST^MCKESSON^^^
  • msgParser test.hl7 | egrep 'PID.05'
     This shows PID.05 only.
    PID.05: TEST^MCKESSON^^^
    PID.05: TEST^TERESA^X^^
    PID.05: HORIZON^TEST^^^
    PID.05: HORIZON^TEST^^^
    PID.05: HORIZON^TEST^^^
    PID.05: HORIZON^TEST^^^
    PID.05: HORIZON^TEST^^^
    PID.05: HORIZON^TEST^^^
    PID.05: TEST^MCKESSON^^^
  • msgParser test.hl7 | egrep 'MSH.09' | sort -u
    This gets a list of event types, sorted uniquely.
    MSH.09: ADT^A01
    MSH.09: ADT^A05
    MSH.09: ADT^A08
    MSH.09: ADT^A28
    MSH.09: ADT^A31
    MSH.09: ORR^O02
  • msgParser test.hl7 | egrep '2008' | sort -u
    This searches for date stamps in the message.
    EVN.02: 20080905085812
    EVN.02: 20080905085858
    EVN.02: 20080905085859
    EVN.02: 20080905090024
    MSH.07: 20080905060049
    MSH.07: 20080905061606
    MSH.07: 20080905085815
    MSH.07: 20080905085900
    MSH.07: 20080905090026
    MSH.07: 20080905103736
    OBR.27: 1^ONCE^^200809050600^^9
    OBR.27: 1^ONCE^^200809050715^^9
    OBR.27: 1^ONCE^^200809051036^^9
    ORC.07: 1^ONCE^^200809050600^^9
    ORC.07: 1^ONCE^^200809050715^^9
    ORC.07: 1^ONCE^^200809051036^^9
    ORC.09: 200809050600
    ORC.09: 200809050615
    ORC.09: 200809051036
    PV1.44: 200809050859
    PV1.44: 200809150700
  • msgParser test.hl7 | egrep 'ORC.01'
    This only shows ORC.01. This is simpler to search for with the left padding. ORC.01 would match onORC.11, ORC.12, ORC.13, and so on.
    ORC.01: NA
    ORC.01: NA
    ORC.01: NA
  • msgParser test.hl7 | egrep 'ORC.10'
    ORC.10: 350987^MAR
    ORC.10: 350987^MAR
    ORC.10: 350829^STE
  • msgParser test.hl7 | egrep 'ORC.(01|10)|OBR.16'
    This shows ORC.01, ORC.10, and OBR.16.
    ORC.01: NA
    ORC.10: 350987^MAR
    OBR.16: 00428^PETERS^WALTER^R^^^MD
    ORC.01: NA
    ORC.10: 350987^MAR
    OBR.16: 00431^SEE^WILLIAM^M^^^MD
    ORC.01: NA
    ORC.10: 350829^STE
    OBR.16: 00428^PETERS^WALTER^R^^^MD