Sign in to follow this  
Followers 0
RyukShini

Splitting string/file

18 posts in this topic

I have a string looking like this:
Outsider:myemail@myemail.com:26e0112200304f6d7598f6bd90a8478d:_KPQX)B7C0IX~!QgqG*V*){X<71O{{

This is how my database looks, I would like to split my file in to
User
Email
Hash:Salt

The reason why I wish to do this is to have an easier view of my users, I may also want to encrypt passwords even further and I cannot do this with a string looking like that!

Thanks in advance.

Share this post


Link to post
Share on other sites

#2 ·  Posted (edited)

#include <Array.au3>

$string="Outsider:myemail@myemail.com:26e0112200304f6d7598f6bd90a8478d:_KPQX)B7C0IX~!QgqG*V*){X<71O{{"
$split=StringSplit($string,":")
_ArrayDisplay($split)

NB: If colons can appear in your salt, you would have to catenate $split entries 4-N if split[0]>4.

Edited by RTFC

Share this post


Link to post
Share on other sites
8 minutes ago, RTFC said:
#include <Array.au3>

$string="Outsider:myemail@myemail.com:26e0112200304f6d7598f6bd90a8478d:_KPQX)B7C0IX~!QgqG*V*){X<71O{{"
$split=StringSplit($string,":")
_ArrayDisplay($split)

NB: If colons can appear in your salt, you would have to catenate $split entries 4-N if split[0]>4.

Hi thanks, it works!
but what if a string contains this:
tester:test@test.com:9b6af4443774867abc06358645aa4cdb:D>!+\,6gR!sJ+{:q5Z]Wy2/!q:0PXZ

As you can see their are : within the salt.
Thanks in advance!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Please note my comment below the code snippet in my previous post. In short:

1. check if $split[0]>4; if not, you're done

2. add  colon plus next part of salt to start of salt for all parts >4

#include <Array.au3>

$string="tester:test@test.com:9b6af4443774867abc06358645aa4cdb:D>!+\,6gR!sJ+{:q5Z]Wy2/!q:0PXZ"
$split=stringsplit($string,":")
For $sc=5 to $split[0]
    $split[4]&=":"&$split[$sc]
Next
Redim $split[5]
_ArrayDisplay($split)

So the For...Next loop is carried out only if $split[0] is at least 5.

Alternatively you could store the salt as a hexstring, and convert to binary where necessary.

Edited by RTFC

Share this post


Link to post
Share on other sites
7 minutes ago, RTFC said:

Please note my comment below the code snippet. In short:

1. check if $split[0]>4, if not, you're done

2. add  colon plus next part of salt to start of salt for all parts >4

#include <Array.au3>

$string="tester:test@test.com:9b6af4443774867abc06358645aa4cdb:D>!+\,6gR!sJ+{:q5Z]Wy2/!q:0PXZ"
$split=stringsplit($string,":")
For $sc=5 to $split[0]
    $split[4]&=":"&$split[$sc]
Next
Redim $split[5]
_ArrayDisplay($split)

So the For...Next loop is carried out only if $split[0] is at least 5.

Works great.
Now if  I have a file with say 500 lines, I could write 500 instead of 5, but couldn't I load the line number in a variable if I am unsure how many lines their is in the file I wish to organize?

Thanks in advance.

Share this post


Link to post
Share on other sites

Please read up on FileReadLine and _FileWriteFromArray in the Help file. Read an entry, split it, store it.

You can of course also store the 1D array $split into a new row of your own 2D array in each read-iteration.

That should be sufficient to get you started, methinks.

Share this post


Link to post
Share on other sites
Just now, RTFC said:

Please read up on FileReadLine and _FileWriteFromArray in the Help file. Read an entry, split it, store it.

You can of course also store the 1D array $split into a new row of your own 2D array in each read-iteration.

That should be sufficient to get you started, methinks.

#include <Array.au3>

$file = FileRead("new.txt")
$split=StringSplit($file,":")
For $sc=5 to $split[0]
    $split[5]&=":"&$split[$sc]
Next
Redim $split[5]
_ArrayDisplay($split)

It works fine, but this still gives me 5 only.
I want it to read all of the file, so maybe I should use [$i] or something?
maybe.

For $i = 1 to $line or something similar?
Thanks in advance.

Share this post


Link to post
Share on other sites
20 minutes ago, RTFC said:

Check FileReadLine, not FileRead.

I made it work, but now it will only load 4 lines.

028bfeada69e7ea1cc1f1bc0657e2d0c.png

Now the first 4 lines works fine, but the rest is blank??
Thanks in advance
 

#include <Array.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
$file = "new.txt"
Local $hFileOpen = FileOpen("new.txt", $FO_READ)

Local $sFileRead = FileReadLine($hFileOpen, 1)
$split=StringSplit($sFileRead,":")
For $sc=100 to $split[0]
    $split[100]&=":"&$split[$sc]
Next
Redim $split[100]
_ArrayDisplay($split)

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Okay, one last try.:x

As I told you before ("in each read iteration"), you need to repeat the stringsplit for every line of your file, and store the data per line. Something like this (haven't tested it).

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

$file = "new.txt"
$lines=_FileCountLines($file)
global $list[$lines+1][4]
Local $hFileOpen = FileOpen($file, $FO_READ)

Local $curline=0
Local $sFileRead = FileReadLine($hFileOpen, 1)
while not @error
    $split=StringSplit($sFileRead,":")
    For $sc=5 to $split[0]
        $split[4]&=":"&$split[$sc]
    Next

    $curline+=1
    for $sc=1 to 4
        $list[$curline][$sc-1]=$split[$sc]
    next
    $sFileRead = FileReadLine($hFileOpen)
WEnd
FileClose($hFileOpen)
$list[0]=$lines
_ArrayDisplay($list)

 

Edited by RTFC
bug

Share this post


Link to post
Share on other sites
6 minutes ago, RTFC said:

Okay, one last try.:x

As I told you before ("in each read iteration"), you need to repeat the stringsplit for every line of your file, and store the data per line. Something like this (haven't tested it).

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

$file = "new.txt"
$lines=_FileCountLines($file)
global $list[$lines+1][4]
Local $hFileOpen = FileOpen($file, $FO_READ)

Local $curline=0
Local $sFileRead = FileReadLine($hFileOpen, 1)
while not @error
    $split=StringSplit($sFileRead,":")
    For $sc=5 to $split[0]
        $split[4]&=":"&$split[$sc]
    Next

    $curline+=1
    for $sc=1 to 4
        $list[$curline][$sc-1]=$split[$sc]
    next
    $sFileRead = FileReadLine($hFileOpen)
WEnd
FileClose($hFileOpen)
$list[0]=$lines
_ArrayDisplay($list)

 

Sorry, will figure it out!
Thanks a lot for the time and effort you put in this.

Share this post


Link to post
Share on other sites

You're welcome, RyukShini.:)

I'm sure you'll get the hang of it. If not, I would suggest you study the Help example of FileReadLine and read up on 2D arrays.

Good luck,

RT

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Hi.

I'd like to suggest, to read the full file to an array using _filereadtoarray(). Then loop through this array to process your lines. This will give you a start, I think:

#include <file.au3>
#include <array.au3>


dim $aLines
dim $file="C:\temp\your-input.txt"

$f=FileOpen($file,2+8)

FileWriteLine($f,"Outsider:myemail@myemail.com:26e0112200304f6d7598f6bd90a8478d:_KPQX)B7C0IX~!QgqG*V*){X<71O{{")
FileWriteLine($f,"Insider:InEmail@myemail.com:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:111111111111111111111111111111")
FileWriteLine($f,"upsider:UpEmail@myemail.com:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb:222222222222222222222222222222")
FileClose($f)


_FileReadToArray($File,$aLines)

if IsArray($aLines) Then
    _ArrayDisplay($aLines)
Else
    MsgBox(0,"","not an array")
    Exit
EndIf


for $i = 1 to $aLines[0]
    if StringStripWS($aLines[$i],8)="" Then
        MsgBox(0,"Empty line (or blanks only)","Line #" & $i)
        ContinueLoop
    EndIf
    $aFoo=StringSplit($aLines[$i],":")
    if IsArray($aFoo) Then
        _ArrayDisplay($aFoo)
    Else
        MsgBox(0,"Line " & $i,"The result isn't an array!" & @CRLF & _
        "String:" & @CRLF & _
        $aLines[$i])
    EndIf
Next

regards, Rudi.

 

<edit: Can the salt have ":" in its string?>

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

i also sugest reading the full fill:

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

Global $aAccounts, $iFilledRows

_FileReadToArray('accounts.txt', $aAccounts, $FRTA_NOCOUNT)
;, ':');using splitting in Cols fails on files with diferent colcounts in line
If Not @error Then
    _ArrayColInsert($aAccounts, 1)
    _ArrayColInsert($aAccounts, 1)
    _ArrayColInsert($aAccounts, 1)
    For $iRows = 0 To UBound($aAccounts) - 1
        If Not StringIsSpace($aAccounts[$iRows][0]) Then
            $aSplit = StringSplit($aAccounts[$iRows][0], ':', 2)
            For $iCols = 0 To 2
                $aAccounts[$iRows][$iCols] = $aSplit[$iCols]
            Next
            $sSalt = ''
            For $iCols = 3 To UBound($aSplit) - 1
                $sSalt &= $aSplit[$iCols]
            Next
            $aAccounts[$iRows][3] = $sSalt
            $iFilledRows += 1
        EndIf
    Next
    ReDim $aAccounts[$iFilledRows][4]
EndIf
_ArrayDisplay($aAccounts)

 

Share this post


Link to post
Share on other sites
19 hours ago, AutoBert said:

i also sugest reading the full fill:

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

Global $aAccounts, $iFilledRows

_FileReadToArray('accounts.txt', $aAccounts, $FRTA_NOCOUNT)
;, ':');using splitting in Cols fails on files with diferent colcounts in line
If Not @error Then
    _ArrayColInsert($aAccounts, 1)
    _ArrayColInsert($aAccounts, 1)
    _ArrayColInsert($aAccounts, 1)
    For $iRows = 0 To UBound($aAccounts) - 1
        If Not StringIsSpace($aAccounts[$iRows][0]) Then
            $aSplit = StringSplit($aAccounts[$iRows][0], ':', 2)
            For $iCols = 0 To 2
                $aAccounts[$iRows][$iCols] = $aSplit[$iCols]
            Next
            $sSalt = ''
            For $iCols = 3 To UBound($aSplit) - 1
                $sSalt &= $aSplit[$iCols]
            Next
            $aAccounts[$iRows][3] = $sSalt
            $iFilledRows += 1
        EndIf
    Next
    ReDim $aAccounts[$iFilledRows][4]
EndIf
_ArrayDisplay($aAccounts)

 

Thanks, it works as intended, but I don't know why, I just can't seem to understand how to get it to display all of the file....
right now it still only displayed 39 results :S
Thanks in advance.

Share this post


Link to post
Share on other sites
20 hours ago, rudi said:

Hi.

I'd like to suggest, to read the full file to an array using _filereadtoarray(). Then loop through this array to process your lines. This will give you a start, I think:

#include <file.au3>
#include <array.au3>


dim $aLines
dim $file="C:\temp\your-input.txt"

$f=FileOpen($file,2+8)

FileWriteLine($f,"Outsider:myemail@myemail.com:26e0112200304f6d7598f6bd90a8478d:_KPQX)B7C0IX~!QgqG*V*){X<71O{{")
FileWriteLine($f,"Insider:InEmail@myemail.com:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:111111111111111111111111111111")
FileWriteLine($f,"upsider:UpEmail@myemail.com:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb:222222222222222222222222222222")
FileClose($f)


_FileReadToArray($File,$aLines)

if IsArray($aLines) Then
    _ArrayDisplay($aLines)
Else
    MsgBox(0,"","not an array")
    Exit
EndIf


for $i = 1 to $aLines[0]
    if StringStripWS($aLines[$i],8)="" Then
        MsgBox(0,"Empty line (or blanks only)","Line #" & $i)
        ContinueLoop
    EndIf
    $aFoo=StringSplit($aLines[$i],":")
    if IsArray($aFoo) Then
        _ArrayDisplay($aFoo)
    Else
        MsgBox(0,"Line " & $i,"The result isn't an array!" & @CRLF & _
        "String:" & @CRLF & _
        $aLines[$i])
    EndIf
Next

regards, Rudi.

 

<edit: Can the salt have ":" in its string?>

Yes it can have ":" in the salt :/

Share this post


Link to post
Share on other sites
22 hours ago, RTFC said:

You're welcome, RyukShini.:)

I'm sure you'll get the hang of it. If not, I would suggest you study the Help example of FileReadLine and read up on 2D arrays.

Good luck,

RT

Thanks, doing this now.
Its strange cos i feel i've done more complicated stuff in the past, but this one is just giving me a headache :/

Share this post


Link to post
Share on other sites

You could do it using a regex, it can be more accurate in case of tricky splits
(error checking missing in the code below)

#include <Array.au3>

$str = "this is a test:indeed" & @crlf & _
    "Outsider:myemail@myemail.com:26e0112200304f6d7598f6bd90a8478d:_KPQX)B7C0IX~!QgqG*V*){X<71O{{" & _ 
    @crlf & _
    "Insider:InEmail@myemail.com:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:1111111111111:1111111:111111:1111" & _
    @crlf & _ 
    "tester:test@test.com:9b6af4443774867abc06358645aa4cdb:D>!+\,6gR!sJ+{:q5Z]Wy2/!q:0PXZ"

$tmp = StringRegExp($str, '(?m)^(.+?):(.+?):(.*)\R?', 3)
Local $res[UBound($tmp)/3][3]
   For $i = 0 to UBound($tmp)-1 step 3
       For $j = 0 to 2
          $res[$i/3][$j] = $tmp[$i+$j]
       Next
   Next
_ArrayDisplay($res)

 

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

    • XinYoung
      By XinYoung
      Hello again,
      I'm trying to use the FileMove function to rename a bunch of text files.
      Some of the files have "_1" at the end of their name, for example, "File123_1.txt". I want to remove the "_1" so it would become "File123.txt".
      What I currently have...
              FileMove($sSource & "\*_1.txt", $sDestination & "\*.txt")
      But since I'm using a wildcard, it doesn't appear to be working. I think it's just replacing .txt with .txt. Am I going about this the wrong way? How can I use wildcards and still accomplish this?
      Any help is greatly appreciated ^__^
    • ur
      By ur
      I have date in the string format as "DD-MM-YYYY".
       
      I need to get yesterday's date from it.
      I tried converting this from _DateTimeFormat but not working.
      Is there any direct UDF available to get this.?
    • lewisg
      By lewisg
      Suddenly a RunWait command has stop working after 2 years of no errors, issues, or problems. The code uses RunWait to start Plink.exe, a command-line remote connection tool similar to UNIX ssh. I'm using it to ssh to a linux (Centos) machine, run a Perl script, and redirect the output to a file on a PC running the AutoIt script.  
       
      $FilePath = "C:\AutoIT\LED" $FilePathPlus = $FilePath & "\plink.exe" $Code1 = RunWait(@ComSpec & " /c " & $FilePathPlus & " -ssh -l root -pw ?????? 10.170.4.163 /usr/local/nagios/etc/led.pl > C:\AutoIT\LED\led.txt ", @SW_SHOW) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : @ComSpec & " /c " & $FilePathPlus & " -ssh -l root -pw ????? 10.170.4.163 /usr/local/nagios/etc/led.pl > C:\AutoIT\LED\led.txt " = ' & @ComSpec & " /c " & $FilePathPlus & " -ssh -l root -pw ?????? 10.170.4.163 /usr/local/nagios/etc/led.pl > C:\AutoIT\LED\led.txt " & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console The ConsoleWrite output when cut-n-pasted into a DOS box produces the expected file so I know it works. I've also tried many variations of the function moving, adding, and changing the " and ' (quote) marks. Also tried it without the @ComSpec macro and other related functions...e.g.  ShellExecuteWait , etc.
      The PC is a Windows 10 64bit and AutoIT is version 3.3.14.2.
      Searching here and Google has not yielded any clues that helped. 
    • rkr
      By rkr
      Hi, i want to read a particular string from a text file using autoit. i wish to read it without explicitly opening the text file. the copied string should be then transferred to an excelbook (again, no need to explicitly open the excel book)... 

      with reference to my screenshot attached, my input to the  'script'' is going to be 0017-0008, and the script should copy the highlighted two lines from the input file to excel
      thanks

    • rudi
      By rudi
      Hello.
      I'm too stupid to see my mistake:
      To investigate the internal "dictionary" of TIFF files I'd like to read in the files in binary mode and to check, if there are more than one pages "in" this TIFF.
      Notepad++, "View as Hex" is presenting the first bytes as "49 49 2a 20 08 20 20 20 12" for the TIF attached to this posting
      The "TIFF Header Format" is easy:
      Offset 00h, 2 Byte = Byte Order, "II"=intel, "MM"=motorola. (I = 0x49)
      --> II
      Offset 02h, 2 Byte = Version Nr.
      Offset 04h, 4 Byte = pointer to first IFD entry
      Description of TIFF header: https://www.awaresystems.be/imaging/tiff/faq.html#q3
       

      Howto read and analyse the binary content correctly? This is my messy, not operational code:
       
      $sampleTiff="H:\daten\tif\11\11\111111.TIF" $h=FileOpen($sampleTiff,16) $content=FileRead($h) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $content = ' & $content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console FileClose($h) $type=VarGetType($content) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $type = ' & $type & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $ToString=BinaryToString($content) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $ToString = ' & $ToString & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite(@CRLF & @CRLF) $content=StringTrimLeft($content,2) ; cut off the leading "0x" ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $content = ' & $content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console for $i = 1 to 8 step 8 $next=StringMid($content,$i,2) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $next = ' & $next & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $Chr=BinaryToString($next) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Chr = ' & $Chr & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite(@CRLF & "---" & @CRLF) Next Regards, Rudi.
      111111.TIF