Jump to content

Extracting content for a text file


koenp
 Share

Recommended Posts

Hello everybody,

I am currently working on a script where the goal is to  extract certain content out of a txt will and put this in a other file

EXAMPLE OF TXT FILE

CHU LIEGE                                                                               SART TILMAN - LIEGE
Name: ABC CDE                   Patient ID: 20120831
Sample ID: 200604003705              Sample Type:Serum                      Dilution Factor: 1
Priority: Routine                    Rack Position: QK000137-6              Completed On: 2013-06-05 15:35:54
Operator: C135669REGI                Instrument ID: DV330681                Aliquot Creation Time: 2013-06-05 14:40:37
Comment:
   Method           Result   Units   RefRange        Flag                Comment                          Reagent
   IGG3             0.389    g/L     0.11 - 0.85                                                          12292MA
   IGG4                              0.03 - 2.01     Below Assay Range                                    12324MA
   IGG2                              1.69 - 7.86     Below Assay Range                                    12310MA
   IGG4             0.009    g/L     0.03 - 2.01                         Diluted                          12324MA
                                                                         Below Reference
                                                                         Range
   IGG2             0.368    g/L     1.69 - 7.86                         Diluted                          12310MA
                                                                         Below Reference
                                                                         Range
NON-STANDARD CONFIGURATION                                                                   Printed 2013-06-05 15:35:57

I am only interested starting from line 9 (IGG3...)

My goal is to have following structure:
IGG3;0.389;g/L;0.11-.085;;;12292MA
IGG4;;;0.03-2.01;Below Assay Range;;12324MA
IGG2;;;1.69-7.86:Below Assasy Range;;12310MA
IGG4;.009;g/L;0.03-2.01;;Diluted;12324MA

Here is the code that I currently have created:

Global $SampleID = StringRegExp(FileRead($file), "Sample ID:" & '\s+(\S+)', 3)
Global $instr = StringRegExp(FileRead($file), "Instrument ID:" & '\s+(\S+)', 3)
Global $type = StringRegExp(FileRead($file), "Type:" & '\S+', 3)


Global $end = StringRegExp(FileRead($file), "Reagent" & '\s+(\S+)', 3)
Global $result1 = StringRegExp(FileRead($file), $end[0] & '\s+(\S+)', 3)
Global $unit1 = StringRegExp(FileRead($file), $result1[0] & '\s+(\S+)', 3)
Global $normals1a = StringRegExp(FileRead($file), $unit1[0] & '\n+', 3)
Global $normals1b = StringRegExp(FileRead($file), $normals1a[0] & '\s+(\S+)', 3)
Global $normals1c = StringRegExp(FileRead($file), $normals1b[0] & '\s+(\S+)', 3)

Local $hFileOpen = FileOpen($patientresult, $FO_APPEND)
    FileWriteLine($hFileOpen, $date & " ; " & $SampleID[0] & " ; " & $instr[0] & " ; " & $type[0] & " ; " & $end[0] & " ; " & $result1[0] & " ; " & $unit1[0] & $normals1a[0] & " ; " & $normals1b[0] & " ; " & @CRLF)
 FileClose($hFileOpen)

 

Can anybody help me with this or give me some advice how to complete this task?

 

Many thanks in advanced and for having a look in to it

 

 

 

Link to comment
Share on other sites

  • Moderators

koenp,

My effort:

$sText = "CHU LIEGE                                                                               SART TILMAN - LIEGE" & @CRLF & _
        "Name: ABC CDE                   Patient ID: 20120831" & @CRLF & _
        "Sample ID: 200604003705              Sample Type:Serum                      Dilution Factor: 1" & @CRLF & _
        "Priority: Routine                    Rack Position: QK000137-6              Completed On: 2013-06-05 15:35:54" & @CRLF & _
        "Operator: C135669REGI                Instrument ID: DV330681                Aliquot Creation Time: 2013-06-05 14:40:37" & @CRLF & _
        "Comment:" & @CRLF & _
        "   Method           Result   Units   RefRange        Flag                Comment                          Reagent" & @CRLF & _
        "   IGG3             0.389    g/L     0.11 - 0.85                                                          12292MA" & @CRLF & _
        "   IGG4                              0.03 - 2.01     Below Assay Range                                    12324MA" & @CRLF & _
        "   IGG2                              1.69 - 7.86     Below Assay Range                                    12310MA" & @CRLF & _
        "   IGG4             0.009    g/L     0.03 - 2.01                         Diluted                          12324MA" & @CRLF & _
        "                                                                        Below Reference" & @CRLF & _
        "                                                                        Range" & @CRLF & _
        "   IGG2             0.368    g/L     1.69 - 7.86                         Diluted                          12310MA" & @CRLF & _
        "                                                                        Below Reference" & @CRLF & _
        "                                                                        Range" & @CRLF & _
        "NON-STANDARD CONFIGURATION                                                                   Printed 2013-06-05 15:35:57"

$aRet = StringRegExp($sText, "(IGG\d|\d\.\d{3}|\d.\d\d\s-\s\d\.\d\d|\d{5}MA)", 3)

$sRet = ""
For $i = 0 To UBound($aRet) - 1
    If StringInStr($aRet[$i], "IGG") Then
        $sRet &= $aRet[$i] & ";"
        $i += 1
        If Stringinstr($aRet[$i], "-") Then
            $sRet &= ";;" & $aRet[$i] & ";Below Assay Range;;"
            $i += 1
        Else
            $sRet &= $aRet[$i] & ";g/L;" & $aRet[$i + 1] & ";;;"
            $i += 2
        EndIf
        $sRet &= $aRet[$i] & @CRLF
    EndIf
Next

ConsoleWrite($sRet & @CRLF)

Now wait for real RegEx guru to do it in one line!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks a lot Melba23,

this looks very close to what I am looking for, just maybe some small adaptations:

The IGG (testcode)  is variable can be anything the same for the g/L (unit).

Is this possible to adapt?

for the rest this looks exactly what I am looking for

 

Many thanks for your effort !!

Koen

 

Link to comment
Share on other sites

  • Moderators

koenp,

Quote

The IGG (testcode)  is variable can be anything the same for the g/L (unit).

Once again we find that example in the original question does not actually reflect the real world case - and so once again we waste time coming up with a solution which does not fit the actual requirement. Point taken?

A different approach is needed if there is no obvious pattern to some of the elements - perhaps this:

#include <Array.au3>
#include <StringConstants.au3>

$sText = "CHU LIEGE                                                                               SART TILMAN - LIEGE" & @CRLF & _
        "Name: ABC CDE                   Patient ID: 20120831" & @CRLF & _
        "Sample ID: 200604003705              Sample Type:Serum                      Dilution Factor: 1" & @CRLF & _
        "Priority: Routine                    Rack Position: QK000137-6              Completed On: 2013-06-05 15:35:54" & @CRLF & _
        "Operator: C135669REGI                Instrument ID: DV330681                Aliquot Creation Time: 2013-06-05 14:40:37" & @CRLF & _
        "Comment:" & @CRLF & _
        "   Method           Result   Units   RefRange        Flag                Comment                          Reagent" & @CRLF & _
        "   IGG3             0.389    g/L     0.11 - 0.85                                                          12292MA" & @CRLF & _
        "   Random                            0.03 - 2.01     Below Assay Range                                    12324MA" & @CRLF & _
        "   IGG2                              1.69 - 7.86     Below Assay Range                                    12310MA" & @CRLF & _
        "   ABC4             0.009    random  0.03 - 2.01                         Diluted                          12324MA" & @CRLF & _
        "                                                                        Below Reference" & @CRLF & _
        "                                                                        Range" & @CRLF & _
        "   IGG2             0.368    g/L     1.69 - 7.86                         Diluted                          12310MA" & @CRLF & _
        "                                                                        Below Reference" & @CRLF & _
        "                                                                        Range" & @CRLF & _
        "NON-STANDARD CONFIGURATION                                                                   Printed 2013-06-05 15:35:57"

; Simulate reading file into an array
$aLines = StringSplit($stext, @CRLF, $STR_ENTIRESPLIT)
; Find "Method" line
$iStart = _ArraySearch($aLines, "Method", 0, 0, 0, 1) + 1
; Find "NON-STANDARD" line
$iEnd = _ArraySearch($aLines, "NON-STANDARD", 0, 0, 0, 1) - 1
; Construct return
$sRet = ""
For $i = $iStart To $iEnd
    ; Replace spaces in line with "|"
    $sLine = StringRegExpReplace($aLines[$i], "\s+", "|")
    ; Count instances
    StringReplace($sLine, "|", "")
    ; Valid line if at least 3
    If @extended > 2 Then
        $aSplit = StringSplit($sLine, "|")
        ; See which form line takes and contruct accordingly
        If $aSplit[6] = "-" Then
            $sRet &= $aSplit[2] & ";" & $aSplit[3] & ";" & $aSplit[4] & ";" & $aSplit[5] & "-" & $aSplit[7] & ";;;" & $aSplit[$aSplit[0]] & @CRLF
        Else
            $sRet &= $aSplit[2] & ";;;" & $aSplit[3] & "-" & $aSplit[5] & ";Below Assay Range;;" & $aSplit[$aSplit[0]] & @CRLF
        EndIf
    EndIf
Next

ConsoleWrite($sRet & @CRLF)

That works on my self-modified data - does it on the real world case? If not then please post some representative data.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I highly suspect a tab delimitation in this txt file. In this case it would obviously make brain-storming less painful  :)
Could you post the real file ? 

Edit
Without more infos, and empirically assuming that *4 or more spaces* will work as delimiter, here are my 2 cents
[btw Melba, your code skips the "diluted" :) ]

#include <Array.au3>
#include <StringConstants.au3>
#include <File.au3>

$sText = "CHU LIEGE                                                                               SART TILMAN - LIEGE" & @CRLF & _
        "Name: ABC CDE                   Patient ID: 20120831" & @CRLF & _
        "Sample ID: 200604003705              Sample Type:Serum                      Dilution Factor: 1" & @CRLF & _
        "Priority: Routine                    Rack Position: QK000137-6              Completed On: 2013-06-05 15:35:54" & @CRLF & _
        "Operator: C135669REGI                Instrument ID: DV330681                Aliquot Creation Time: 2013-06-05 14:40:37" & @CRLF & _
        "Comment:" & @CRLF & _
        "   Method           Result   Units   RefRange        Flag                Comment                          Reagent" & @CRLF & _
        "   IGG3             0.389    g/L     0.11 - 0.85                                                          12292MA" & @CRLF & _
        "   Random                            0.03 - 2.01     Below Assay Range                                    12324MA" & @CRLF & _
        "   IGG2                              1.69 - 7.86     Below Assay Range                                    12310MA" & @CRLF & _
        "   ABC4             0.009     random     0.03 - 2.01                         Diluted                          12324MA" & @CRLF & _
        "                                                                        Below Reference" & @CRLF & _
        "                                                                        Range" & @CRLF & _
        "   IGG2             0.368    g/L     1.69 - 7.86                         Diluted                          12310MA" & @CRLF & _
        "                                                                        Below Reference" & @CRLF & _
        "                                                                        Range" & @CRLF & _
        "NON-STANDARD CONFIGURATION                                                                   Printed 2013-06-05 15:35:57"


; $sText = FileRead ... etc
$sText = StringRegExpReplace($sText, '(?s).*Reagent\s*(.*?)NON-STANDARD.*', "$1")
;Msgbox(0,"", $sText)
$res = StringRegExp($sText, '(?m)^(?!\s+Below|\s+Range)\s*(\N+)\R?', 3)
;_ArrayDisplay($res)

For $i = 0 to UBound($res)-1
   $tmp = StringRegExp($res[$i], '\s*(.*?\S)(?=\s{4,}|$)', 3)
;_ArrayDisplay($tmp)
   If $tmp[2] = "Below Assay Range" Then 
          $res[$i] = $tmp[0] & ";;;" & $tmp[1] & ";Below Assay Range;;" & $tmp[3]
   ElseIf $tmp[4] = "Diluted" Then 
          $res[$i] = $tmp[0] & ";" & $tmp[1] & ";" & $tmp[2] & ";" & $tmp[3] & ";;Diluted;" & $tmp[5]
   Else
          $res[$i] = $tmp[0] & ";" & $tmp[1] & ";" & $tmp[2] & ";" & $tmp[3] & ";;;" & $tmp[4]
   EndIf
Next

FileDelete("output.txt")
_FileWriteFromArray("output.txt", $res) 

; verification
Sleep(100)
Local $aOutput
_FileReadToArray("output.txt", $aOutput, 0, ";")
_ArrayDisplay($aOutput)

 

Edited by mikell
Link to comment
Share on other sites

  • Moderators

koenp,

No tabs, so what I posted above works nicely on the real files:

#include <Array.au3>
#include <File.au3>
#include <StringConstants.au3>

$aList = _FileListToArray(@ScriptDir, "pat_layout*.txt", 1)

For $i = 1 To $aList[0]

    $sRet = _ParseFile($aList[$i])
    ConsoleWrite($sRet & @CRLF)

Next

Func _ParseFile($sFile)

    ; Read file into an array
    Local $aLines
    _FileReadToArray($sFile, $aLines)
    ; Find "Method" line
    $iStart = _ArraySearch($aLines, "Method", 0, 0, 0, 1) + 1
    ; Find "NON-STANDARD" line
    $iEnd = _ArraySearch($aLines, "NON-STANDARD", 0, 0, 0, 1) - 1
    ; Construct return
    $sExtract = ""
    For $i = $iStart To $iEnd
        ; Replace spaces in line with "|"
        $sLine = StringRegExpReplace($aLines[$i], "\s+", "|")
        ; Count instances
        StringReplace($sLine, "|", "")
        ; Valid line if at least 3
        If @extended > 2 Then
            $aSplit = StringSplit($sLine, "|")
            ; See which form line takes and contruct accordingly
            If $aSplit[6] = "-" Then
                $sExtract &= $aSplit[2] & ";" & $aSplit[3] & ";" & $aSplit[4] & ";" & $aSplit[5] & "-" & $aSplit[7] & ";;;" & $aSplit[$aSplit[0]] & @CRLF
            Else
                $sExtract &= $aSplit[2] & ";;;" & $aSplit[3] & "-" & $aSplit[5] & ";Below Assay Range;;" & $aSplit[$aSplit[0]] & @CRLF
            EndIf
        EndIf
    Next

    Return $sExtract

EndFunc   ;==>_ParseFile

Results:

RF;431;IU/mL;0.0-15.0;;;13049MA

IGG3;0.389;g/L;0.11-0.85;;;12292MA
IGG4;;;0.03-2.01;Below Assay Range;;12324MA
IGG2;;;1.69-7.86;Below Assay Range;;12310MA
IGG4;0.009;g/L;0.03-2.01;;;12324MA
IGG2;0.368;g/L;1.69-7.86;;;12310MA

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanska a lot @Melba23

 I tested it as well and indeed works fine..

Once the rest of the coding is done I will publish it here so that you  can see the result

@mikell, I like the way you displayed the tables, but is there a easy way to close it popup automaticly like you can do with msgbox ?

 

Many thanks for all your help !!

Koen

Link to comment
Share on other sites

  • Moderators

koenp,

Glad I could help.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

koenp,

If you give your _ArrayDisplay dialog a suitably unique title then you can easily close it programmatically:

#include <MsgBoxConstants.au3>
#include <Array.au3>

$sTitle = "Unique Title"

Global $aArray[][] = [[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5]]

; Run a function every 5 secs
AdlibRegister("_Close_Array_Dialog", 5000)

; Open the dialog
_ArrayDisplay($aArray, $sTitle, Default, 8)

MsgBox($MB_SYSTEMMODAL, "Hi", "The array dialog has closed", 2)

Func _Close_Array_Dialog()

    ; Cancel future calls
    AdlibUnRegister("_Close_Array_Dialog")
    ; Close the dialog
    WinClose($sTitle)

EndFunc

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...