Sign in to follow this  
Followers 0

Parsing Text File

4 posts in this topic

Hi there everyone,

I have a text file that looks something like the following:

Administrator        Guest               Matt                   
jaes                     james               jas                    
jasdfs               jasds               js                 
Matthew              pjaasddasdfs            pjaasdds               
pjads                pjdfs

Notice the odd formatting; there are not always a consistant amount of spaces between terms, I'm looking for a way to break up this text file into the individual terms (in this case user accounts), and not include the spaces. Any ideas?

Thanks for any advice!


Share this post

Link to post
Share on other sites

#2 ·  Posted (edited)

If it's at least "consistant" with two or more spaces separating the columns, below will work.

The example returns a two dimensional array... rows ->[n][n]<- columns

#include <Array.au3> ; for _arraydisplay() func

Global $gs_somefile = "somefile_here.txt"
Global $ga_parsed = _myparse_filefunc($gs_somefile)


Func _myparse_filefunc($s_file)

    Local $s_fread = $s_file
    If FileExists($s_file) Then $s_fread = FileRead($s_file)

    If $s_fread = "" Then Return SetError(1, 0, 0)

    Local $a_lines = StringSplit(StringStripCR($s_fread), @LF)

    Local $a_col = 0
    Local $i_cols = 1, $i_rows = 0, $i_ub
    Local $a_wret[$a_lines[0] + 1][$i_cols]
    For $iline = 1 To $a_lines[0]
        If StringLen(StringStripWS($a_lines[$iline], 8)) = 0 Then ContinueLoop
        $a_col = StringRegExp($a_lines[$iline], "(?s)(.+?)(?:z|s{2,})", 3)
        If @error Then ContinueLoop
        $i_ub = UBound($a_col)
        If $i_ub > $i_cols Then
            $i_cols = $i_ub
            ReDim $a_wret[$a_lines[0] + 1][$i_cols]
        For $iword = 0 To $i_ub - 1
            $a_wret[$i_rows][$iword] = $a_col[$iword]
        $i_rows += 1

    If Not $i_rows Then Return SetError(2, 0, 0)

    ReDim $a_wret[$i_rows][$i_cols]
    Return $a_wret
Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post

Link to post
Share on other sites

#3 ·  Posted (edited)

#include <Array.au3>

$file = @ScriptDir & "test.txt"
$data = FileRead($file)
$data = StringStripWS($data, 7)
$array = StringSplit($data, " " & @CR)


Edited by Spiff59
1 person likes this

Share this post

Link to post
Share on other sites

Thanks guys, both work fine!

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

  • Similar Content

    • ur
      How to get file version
      By ur
      How to get the thrid number of a file version.
      Example: Let's say I have a file with below properties.
      I want only the third number like here it is 1941 as highlighted.
      Is there any built in function to get it??

    • ur
      How to compare file or folder contents
      By ur
      Generally we will use tools like Winmerge or Beyond Compare for this purpose.
      Are there any UDF or libraries available in AutoIT to compare any two files or folder contents.
    • tremolux66
      Exclusive file locking - SOLVED
      By tremolux66
      I've run into the problem where multiple script processes writing to a log file at the same time can interfere with one another. That is, file writes using FileWriteLine are not atomic, so a line of text from one process can have fragments of text from other processes mixed in with it. Exclusive file-locking should take care of the problem.
      Looking through the standard UDFs, I see that _WinAPI_LockFile is available, but according to the MSDN documentation it doesn't block - it returns immediately if the lock can't be obtained - and there doesn't seem to be a UDF for the LockFileEx function. I considered writing my own UDF for LockFileEx, but some aspects (e.g., creating the OVERLAPPED structure and its members) look too complex for the amount of time I have available. I think approximating LockFileEx using LockFile could solve my problem, but I'd like to keep it simple while still working reliably. I'm not confident that (e.g.) a simple retry loop to simulate blocking will be adequate.
      Any suggestions for a simple way to achieve exclusive file-locking with blocking?
    • Muhammad_Awais_Sharif
      String split into char
      By Muhammad_Awais_Sharif
      How To split string into Array of char ?
    • Muhammad_Awais_Sharif
      StringIsDigit Does not work properly
      By Muhammad_Awais_Sharif
      Why this code does not work ?
      $tmp = "1111" If StringIsDigit($tmp) == True Then MsgBox(0,"",StringIsDigit($tmp) == "1") EndIf and this one works 
      $tmp = "1111" If StringIsDigit($tmp) == "1" Then MsgBox(0,"",StringIsDigit($tmp) == "1") EndIf