Sign in to follow this  
Followers 0
jjunior

Extract File Lines To New Files

13 posts in this topic

#1 ·  Posted (edited)

I have the file.txt with the content below

-99.05305    -19.95130    PRODUCT1
-99.05805    -19.99598    PRODUCT2
-59.69598    -00.97666    PRODUCT3
-59.69600    -00.99615    PRODUCT2
-59.59901    -00.93790    MODELO
-59.63899    -00.95839    TEST
-59.58050    -00.98086    MODE
-99.05361    -19.95031    HOMER
-59.79698    -00.00990    MASTER
-59.81611    -00.19885    PRODUCT1
-98.90587    -10.36600    PRODUCT1

And i want to creat files from this file, example:

PRODUCT1.txt with the content below

-59.81611    -00.19885    PRODUCT1
-98.90587    -10.36600    PRODUCT1

-99.05305    -19.95130    PRODUCT1

PRODUCT2.txt with the content below

-59.69600    -00.99615    PRODUCT2

-59.69600    -00.99615    PRODUCT2

I use the script to create a string separating the file, but I do a script for each string below, would do everything in one script?

Sorry my bad english.

Thanks in advanced.

#include <File.au3>

Global $FileArray
$file = @ScriptDir & "\Original.txt" ;arquivo original
$outputfile = @ScriptDir & "\OK\PRODUCT2.txt" ;arquivo de saída
$istring = "PRODUCT2" ; string a ser capturada

_FileReadToArray($file, $FileArray)

$filecount = 0
$sNewFile_Text = ""

ProgressOn("Processando: " & $istring , "Lendo o arquivo Original.txt", "0 Lines")
FileDelete ($outputfile) ;apagar o arquivo antigo (pois se não pagar e acumula os alertas)

For $i = 1 To $FileArray[0]

    If StringInStr($FileArray[$i], $istring) Then

       If $filecount > 0 Then FileWrite($outputfile, $sNewFile_Text)

        $filecount = $filecount + 1
        $sNewFile_Text = $FileArray[$i] & @CRLF
    Else
        $sNewFile_Text &= $FileArray[$i] & @CRLF
    EndIf

       $Percent = Int(($i / $FileArray[0]) * 100)
    ProgressSet($Percent, $Percent & " % Completo do arquivo: " & $outputfile)

Next


ProgressSet(100, "Feito", "Completo")
Sleep(1000)
ProgressOff()
Edited by jjunior

Share this post


Link to post
Share on other sites



jjunior,

Welcome to the AutoIt forums. :)

But in future, please open a new thread like this and do not hijack an old one - particularly when it had only a very tenuous connection to your question. ;)

Give me a moment and I will look at the problem. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

jjunior,

Here is one way you might do it: :)

#include <File.au3>

Global $aOriginal

; Read file into a 2D array
_FileReadToArray(@ScriptDir & "\Original.txt", $aOriginal, $FRTA_COUNT + $FRTA_ENTIRESPLIT, "    ")

; Just for display
_ArrayDisplay($aOriginal, "Original", Default, 8)

; Extract the final elements from each line and get the unique values 
Global $aItems = _ArrayExtract($aOriginal, 1, Default, 2, 2)
$aItems = _ArrayUnique($aItems)

; Again just for display
_ArrayDisplay($aItems, "Unique elements", Default, 8)

; Now for each unique item
For $i = 1 To $aItems[0]
    $sData = ""
    ; Look through the original array
    For $j = 1 To $aOriginal[0][0]
        ; if teh final item matches
        If $aOriginal[$j][2] = $aItems[$i] Then
            ; Add it to the string
            $sData &= $aOriginal[$j][0] & "    " & $aOriginal[$j][1] & "    " & $aOriginal[$j][2] & @CRLF
        EndIf
    Next
    ; Remove the final @CRLF
    $sData = StringTrimRight($sData, 2)
    
    ; This shows what we have in the data in the SciTE console
    ConsoleWrite( $aItems[$i] & ".txt" & @CRLF & $sData & @CRLF & @CRLF)
    
    ; And this is how you could write it to a file
    ;$hFile = FileOpen(@ScriptDir & "\" & $aItems[$i] & ".txt", $FO_OVERWRITE)
    ;FileWrite($hFile, $sData)
    ;FileClose($hFile)
Next
Please ask if you have any questions: :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I want to create other files, with the other end of the line below and others that have on file though I did not show.

Like this

MODELO Fixo - 80.txt

-59.59901    -00.93790    MODELO Fixo - 80 kmh

TEST Fixo - 70.txt
-59.63899    -00.95839    TEST Fixo - 70 kmh

MODE Fixo - 60.TXT
-59.58050    -00.98086    MODE Fixo - 60 kmh

HOMER Fixo - 50.txt
-99.05361    -19.95031    HOMER Fixo - 50 kmh

Edited by jjunior

Share this post


Link to post
Share on other sites

jjunior,

I imagine it is because the _FileReadToArray function does not return an array. What is the actual delimiter between the elements on each line of the file? I used 4 spaces as that was what was in the text you posted - is it perhaps @TAB? :huh:

Could I suggest you post an example copy of the file you are actually using - that way we can see the internal structure. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

jjunior,

As I suspected: @TAB delimited. :)

This works for me:

#include <File.au3>

Global $aOriginal

; Read file into a 2D array
_FileReadToArray(@ScriptDir & "\Original.txt", $aOriginal, $FRTA_COUNT, @TAB)

; Just for display
_ArrayDisplay($aOriginal, "Original", Default, 8)

; Extract the final elements from each line and get the unique values
Global $aItems = _ArrayExtract($aOriginal, 1, Default, 2, 2)
$aItems = _ArrayUnique($aItems)

; Again just for display
_ArrayDisplay($aItems, "Unique elements", Default, 8)

; Now for each unique item
For $i = 1 To $aItems[0]
    $sData = ""
    ; Look through the original array
    For $j = 1 To $aOriginal[0][0]
        ; if teh final item matches
        If $aOriginal[$j][2] = $aItems[$i] Then
            ; Add it to the string
            $sData &= $aOriginal[$j][0] & @TAB & $aOriginal[$j][1] & @TAB & $aOriginal[$j][2] & @CRLF
        EndIf
    Next
    ; Remove the final @CRLF
    $sData = StringTrimRight($sData, 2)

    ; This shows what we have in the data in the SciTE console
    ConsoleWrite( $aItems[$i] & ".txt" & @CRLF & $sData & @CRLF & @CRLF)

    ; And this is how you could write it to a file
    ;$hFile = FileOpen(@ScriptDir & "\" & $aItems[$i] & ".txt", $FO_OVERWRITE)
    ;FileWrite($hFile, $sData)
    ;FileClose($hFile)
Next
M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

This is perfect.

The error was (@TAB delimited)

Thanks M8.

Thanks for the fast help.

Share this post


Link to post
Share on other sites

jjunior,

As always, delighted I could help. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Melba23

 

When a line finish at km/h it´s not possible to save, because the "/".

Have a way to remove "km/h" in the name of file?

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

?

StringReplace()
Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

jjunior,

MikahS' suggestion sounds good to me. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0