Hi Shu,
I did something like that. It is possible to do in Tcl with some assumptions:
1. If you know for sure that OBX-5 is not a repeating filed and does not have any subfields; In this case you can assume that any special character within OBX-5 is just a character that should be escaped.
In this case you should start with escaping “” – otherwise you will end with escaping escaped characters.
2. If you know for sure that OBX-5 is the very last field in the segment; then you can treat all OBX fields starting with OBX-5 to the end of the segment as one filed that has embedded “|”. (If this is not the case then there is no way to know which “|” is part of OBX-5 and which is a field separator).
For example, if your OBX segment looks something like:
OBX|1|TX|||This is OBX-5 field with , ~, & and | embedded as part of the field. This is another | within the same OBX-5|
then the whole string “This is OBX-5 field with , ~, & and | embedded as part of the field. This is another | within the same OBX-5” should be treated as one field.
So, if both assumptions above are true you can do it like this (the code below may be not the best code but it worked in my tests; still you should test it):
# the next command is for testing purposes only
set obx {OBX|1|TX|||This is OBX-5 field with , ~, & and | embedded as part of the field. This is another | within the same OBX-5|}
set sep “|”
set fields [split $obx $sep]
set obx5 [string trimright [join [lrange $fields 5 end] $sep] $sep]
# start with escaping “” – otherwise you will end with escaping escaped characters
regsub -all — {(\)} $obx5 {\E\} obx5
# other escapes can go in any order
regsub -all — {(&)} $obx5 {\T\} obx5
regsub -all — {(~)} $obx5 {\R\} obx5
regsub -all — {(|)} $obx5 {\F\} obx5
regsub -all — {(^)} $obx5 {\S\} obx5]
set obx [join [lreplace $fields 5 end $obx5] $sep]
# debug only:
echo $obx
The code above produces the following output:
OBX|1|TX|||This is OBX-5 field with E, R, T and F embedded as part of the field. This is another F within the same OBX-5
Hope it helps,
Sergey