Jump to content

Splitting string/file


Recommended Posts

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.

Link to comment
Share on other sites

#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
Link to comment
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!

Link to comment
Share on other sites

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
Link to comment
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.

Link to comment
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.

Link to comment
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.

Link to comment
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)

 

Link to comment
Share on other sites

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
Link to comment
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.

Link to comment
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

Link to comment
Share on other sites

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!

Link to comment
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)

 

Link to comment
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.

Link to comment
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 :/

Link to comment
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 :/

Link to comment
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)

 

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...