?
Solved

file not splitting on \r\n - TCL

Posted on 2012-08-24
7
Medium Priority
?
693 Views
Last Modified: 2012-08-25
I am having some trouble parsing my data into lines based on \r\n.  There is a screenshot attached.  As you can see in line 18 on the screen shot this would be the end of a line.  What you can not see is that line #12 would have been the end of the previous line.

So line 13,14,15,16 & 17 should be read in as one line, however they are not.

thanks for the eyes

proc ReadFile {a} {

   set count 0
   
    #open the file for reading
    set f_in [open $a r]
    
    #pass it in
    set f_data [read $f_in]
   
    #now that we have the data, close it
    close $f_in
   
    #split up each message
    set data [split $f_data "\r\n"]
                                      
      #read each piece
      foreach line $data {
                                               
        #define where we are going to write out              
        set outfile [open "c:/Tcl/David12.txt" a+]

 
          #check if this is a MSH line     
          if {[string range $line 0 2] == "MSH"} {
          
            #if this is an MSH line split it and 
            #check to see if it's final or not
            set splitMSH [split $line "|"]
            set type [lindex $splitMSH 18]

              #if it's not a PRELIM message, write it out
              if {$type != "PRELIM"} {
                           puts $outfile $line

#                           puts $outfile $type
              }
        
         }
           
        #close the file
        close $outfile   

      }
   }


ReadFile {c:\Tcl\AllResults.dat}

Open in new window

0
Comment
Question by:gdspeare
  • 4
  • 2
7 Comments
 

Author Comment

by:gdspeare
ID: 38331615
sorry...here's my screenshot.
Image.docx
0
 
LVL 3

Expert Comment

by:fjkaykr11
ID: 38331694
Is that private medical data in that attached file?
0
 

Author Comment

by:gdspeare
ID: 38331738
If you question is: are you breaking hippa rules the answer is no.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 1500 total points
ID: 38332371
Hi, I'm not that experienced in TCL but possibly the foreach is 'splitting' different than you are thinking. For testing, could you post the input file c:\Tcl\AllResults.dat as well?

On second thought: splitting is done on all characters that are provided.

set data [split $f_data "\r\n"]

So it will split lines on \n or \r (even \r\n).
0
 

Author Comment

by:gdspeare
ID: 38332661
Thank gerwinjansen -  I'm going to try what you suggested, but for the time being, i'm going with:

proc ReadFile {a} {

    set typeglob {}
    set MSHLine {}
    set PIDLine {}
    set PV1Line {}
    set ORCLine {}
    set OBRLine {}
    set OBXLine {}
    set count 0
   
    #open the file for reading
    set f_in [open $a r]
    
    #pass it in
    set f_data [read $f_in]
   
    #now that we have the data, close it
    close $f_in
   
   
        #define where we are going to write out              
        set outfile [open "c:/Tcl/David12.txt" a+]
        set typefile [open "c:/Tcl/TypeFile.txt" a+]
           
    #split up each message
    set data [split $f_data "\r\n"]
                                       
      #read each piece
      foreach line $data {

          set type [string range $line 0 2]                        
                                           
          switch -exact -- $type {
          
            MSH {
            set splitMSH [split $line "|"]
            set type [lindex $splitMSH 18]
            
              set typeglob $type
            
              set MSHLine $line
                          
            }
            PID {
            
              set PIDLine $line
            
            }
            PV1 {
            
              set PV1Line $line
                
            }
            ORC {
            
              set ORCLine $line
            
            }
            OBR {
            
              set OBRLine $line
            
            }
            OBX {
            
              set OBXLine $line
              
                             
                if {$typeglob == "FINAL"} {
                
                  puts $outfile $MSHLine\r$PIDLine\r$PV1Line\r$ORCLine\r$OBRLine\r$OBXLine\r\n
                   
                  set MSHLine {}
                  set PIDLine {}
                  set PV1Line {}
                  set ORCLine {}
                  set OBRLine {}
                  set OBXLine {}
                  set type {}
                }
            }
          }                                           
         }
                              
        #close the file
        close $outfile  
              }
   


ReadFile {c:\Tcl\AllResults.dat}

Open in new window

0
 

Author Closing Comment

by:gdspeare
ID: 38332664
thanks for the idea
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 38332979
You're welcome :-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month17 days, 7 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question