Jump to content
tezhihi

Re-Number in txt file

Recommended Posts

tezhihi

i have some the same data in txt file ex below:

data="1"

data="1"

data="1"

data="1"

and i want to replace it as

data="1"

data="2"

data="3"

data="4"

Please provide me how to do that :( i tried with stringregex and find all data to array but when i use _replacestringinfile with loop for $i  = 1 to 4 it doesn't work :(

 

Share this post


Link to post
Share on other sites
KickStarter15

Can you post your script so that we can provide more suggestions and help.^_^


Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Share this post


Link to post
Share on other sites
tezhihi

this is code of this topic :(

#include <Array.au3>
#include <File.au3>
$read = FileRead(@ScriptDir & "\test.txt")
$find = StringRegExp($read, '(datatofindall="[0-9]+")', 3)
For $i = 0 to UBound($find) - 1
   For $j = 1 to 9
      _ReplaceStringInFile(@ScriptDir & "\test.txt", $find[$i], 'datatofindall="' & $j & '"', 1, 1)
   Next
Next

 

the contain of is

datatofindall="1"

datatofindall="1"

datatofindall="1"

datatofindall="1"

datatofindall="1"

datatofindall="1"

datatofindall="1"

datatofindall="1"

datatofindall="1"

and i want to replace it as

datatofindall="1"

datatofindall="2"

datatofindall="3"

datatofindall="4"

datatofindall="5"

datatofindall="6"

datatofindall="7"

datatofindall="8"

datatofindall="9"

 

Share this post


Link to post
Share on other sites
tezhihi
5 minutes ago, KickStarter15 said:

Can you post your script so that we can provide more suggestions and help.^_^

Please check and help me if you can :(

Share this post


Link to post
Share on other sites
jchd

Something like that?

$s = _StringRepeat('datatofindall="1"' & @CRLF, 12)
ConsoleWrite("Before" & @LF & $s & @LF)
$s = Execute('"' & StringRegExpReplace($s, '"\d+"', '""" & _Incr() & """') & '"')
ConsoleWrite("After" & @LF & $s)

Func _Incr()
    Static $n = 0
    $n +=1
    Return $n
EndFunc

 

  • Like 1

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
tezhihi
2 minutes ago, jchd said:

Something like that?

$s = _StringRepeat('datatofindall="1"' & @CRLF, 12)
ConsoleWrite("Before" & @LF & $s & @LF)
$s = Execute('"' & StringRegExpReplace($s, '"\d+"', '""" & _Incr() & """') & '"')
ConsoleWrite("After" & @LF & $s)

Func _Incr()
    Static $n = 0
    $n +=1
    Return $n
EndFunc

 

Yes and i want to replace it in txt file :(

Share this post


Link to post
Share on other sites
Subz

Another way:

#include <Array.au3>

Local $x = 1, $sFindData = 'datatofindall="'
Local $sFileName = @ScriptDir & "\Filename.txt"
Local $aFileName = FileReadToArray($sFileName)
Local $hFileOpen = FileOpen(@ScriptDir & "FileData.txt", 2)
_ArrayDisplay($aFileName, "Before")
For $i = 0 To UBound($aFileName) - 1
    If StringInStr($aFileName[$i], $sFindData) Then
        $aFileName[$i] = $sFindData & $x & '"'
        $x += 1
    EndIf
    FileWrite($hFileOpen, $aFileName[$i] & @CRLF)
Next
FileClose($hFileOpen)
_ArrayDisplay($aFileName, "After")

 

  • Like 1

Share this post


Link to post
Share on other sites
tezhihi
2 minutes ago, Subz said:

Another way:

#include <Array.au3>

Local $x = 1, $sFindData = 'datatofindall="'
Local $sFileName = @ScriptDir & "\Filename.txt"
Local $aFileName = FileReadToArray($sFileName)
Local $hFileOpen = FileOpen(@ScriptDir & "FileData.txt", 2)
_ArrayDisplay($aFileName, "Before")
For $i = 0 To UBound($aFileName) - 1
    If StringInStr($aFileName[$i], $sFindData) Then
        $aFileName[$i] = $sFindData & $x & '"'
        $x += 1
    EndIf
    FileWrite($hFileOpen, $aFileName[$i] & @CRLF)
Next
FileClose($hFileOpen)
_ArrayDisplay($aFileName, "After")

 

oh i understand your code. thanks you so much

Share this post


Link to post
Share on other sites
tezhihi
36 minutes ago, Subz said:

Another way:

#include <Array.au3>

Local $x = 1, $sFindData = 'datatofindall="'
Local $sFileName = @ScriptDir & "\Filename.txt"
Local $aFileName = FileReadToArray($sFileName)
Local $hFileOpen = FileOpen(@ScriptDir & "FileData.txt", 2)
_ArrayDisplay($aFileName, "Before")
For $i = 0 To UBound($aFileName) - 1
    If StringInStr($aFileName[$i], $sFindData) Then
        $aFileName[$i] = $sFindData & $x & '"'
        $x += 1
    EndIf
    FileWrite($hFileOpen, $aFileName[$i] & @CRLF)
Next
FileClose($hFileOpen)
_ArrayDisplay($aFileName, "After")

 

but can not raplace it in txt file example:

this is text datatofindall="1" this is text

this is text datatofindall="1" this is text

this is text datatofindall="1" this is text

this is text datatofindall="1" this is text

this is text datatofindall="1" this is text

this is text datatofindall="1" this is text

if use your code the results will be

datatofindall="1"

datatofindall="2"

datatofindall="3"

datatofindall="4"

datatofindall="5"

datatofindall="6"

Share this post


Link to post
Share on other sites
Subz

What about:

#include <Array.au3>

Local $x = 1, $sFindData = 'datatofindall="1"'
Local $sFileName = @ScriptDir & "\Filename.txt"
Local $aFileName = FileReadToArray($sFileName)
Local $hFileOpen = FileOpen(@ScriptDir & "FileData.txt", 2)
_ArrayDisplay($aFileName, "Before")
For $i = 0 To UBound($aFileName) - 1
    If StringInStr($aFileName[$i], $sFindData) Then
        $aFileName[$i] = StringReplace($aFileName[$i], $sFindData, StringReplace($sFindData, "1", $x))
        $x += 1
    EndIf
    FileWrite($hFileOpen, $aFileName[$i] & @CRLF)
Next
FileClose($hFileOpen)
_ArrayDisplay($aFileName, "After")

 

  • Like 1

Share this post


Link to post
Share on other sites
tezhihi
45 minutes ago, Subz said:

What about:

#include <Array.au3>

Local $x = 1, $sFindData = 'datatofindall="1"'
Local $sFileName = @ScriptDir & "\Filename.txt"
Local $aFileName = FileReadToArray($sFileName)
Local $hFileOpen = FileOpen(@ScriptDir & "FileData.txt", 2)
_ArrayDisplay($aFileName, "Before")
For $i = 0 To UBound($aFileName) - 1
    If StringInStr($aFileName[$i], $sFindData) Then
        $aFileName[$i] = StringReplace($aFileName[$i], $sFindData, StringReplace($sFindData, "1", $x))
        $x += 1
    EndIf
    FileWrite($hFileOpen, $aFileName[$i] & @CRLF)
Next
FileClose($hFileOpen)
_ArrayDisplay($aFileName, "After")

 

Ding Ding. Exactly. Thanks you so much

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

×