Jump to content

Simple Search and Replace


Tom42
 Share

Recommended Posts

I have a series of text files that are all 3202 lines in length. Each line starts with a HEX value

0fff5060e,0 David,1 Aardsma,2 54,3 0,4 0,5 10,6 0,7 29
0fff505da,0 Andy,1 Abad,2 54,3 1,4 1,5 6,6 8,7 25
0fff502d4,0 Jim,1 Abbott,2 15,3 0,4 0,5 10,6 0,7 27
0fff5068b,0 Reggie,1 Abercrombie,2 61,3 0,4 0,5 7,6 11,7 28 
0fff5056f,0 Brent,1 Abernathy,2 15,3 0,4 0,5 3,6 2,7 25
0fff50919,0 Bobby,1 Abreu,2 53,3 1,4 0,5 8,6 8,7 24
0fff5024a,0 Erick,1 Abreu,2 5,3 0,4 0,5 0,6 10,7 25
0fff505d9,0 Michael,1 Abreu,2 28,3 0,4 0,5 2,6 2,7 24
0fff4ff13,0 Tony,1 Abreu,2 5,3 0,4 0,5 5,6 3,7 23

Each hex value needs to be changed (value A to value B). The reason I can not use a standard Search and replace program is because B is sometimes a future A, and after one line is changed I do not want it changed again.

I'm using an ini file for the change list

; IDchanges.ini
; Old Hex ID = New Hex ID

[ID]
0f844e070 = 0b8d68dba
0fc00b45a = 0fc00b45a
0fff4fd32 = 006cb96ac
0fff4fd33 = 0c628e255
0fff4fd34 = 00de04832
0fff4fd35 = 000d2b76a
0fff4fd36 = 0a3c19c92
0fff4fd37 = 0fff4fe05
0fff4fd38 = 00033d596
0fff4fd39 = 09b1c6b60
0fff4fd3a = 03cbc63dd
0fff4fd3b = 0703b1d30
0fff4fd3c = 09d4ab7fa
0fff4fd3d = 0a38dda84
......

Here is the autoit code I came up with

#include <file.au3>
$ini = IniReadSection ("IDchanges.ini" , "ID")
Dim $filelines
$filename = $CmdLine[1]
_FileReadToArray($filename,$filelines)
FileMove($filename,$filename & ".bak")
ProgressOn("Redoing HEX IDs", "Progress", "0 percent")
For $z = 1 To $filelines[0]
    $progresstemp = ($filelines[0] / 100)
    $progress = Round ($z / $progresstemp)
    ProgressSet( $progress, $progress & " percent")
    $i = 1
    For $i = 1 To $ini[0][0]
        If StringInStr ($filelines[$z] , $ini[$i][0]) Then
            $filelines[$z] = StringReplace ( $filelines[$z] , $ini[$i][0] , $ini[$i][1] )
            ExitLoop 1
        EndIf
    Next
Next            
ProgressSet(100 , "Done", "Complete")
sleep(2500)
ProgressOff()
_FileWriteFromArray($filename,$filelines,1)

I'm missing something because only a little more than half of the 3202 changes are made in the text files

Any ideas?

Edited by krawhitham
Link to comment
Share on other sites

I have a series of text files that are all 3202 lines in length. Each line starts with a HEX value

0fff5060e,0 David,1 Aardsma,2 54,3 0,4 0,5 10,6 0,7 29
0fff505da,0 Andy,1 Abad,2 54,3 1,4 1,5 6,6 8,7 25
0fff502d4,0 Jim,1 Abbott,2 15,3 0,4 0,5 10,6 0,7 27
0fff5068b,0 Reggie,1 Abercrombie,2 61,3 0,4 0,5 7,6 11,7 28 
0fff5056f,0 Brent,1 Abernathy,2 15,3 0,4 0,5 3,6 2,7 25
0fff50919,0 Bobby,1 Abreu,2 53,3 1,4 0,5 8,6 8,7 24
0fff5024a,0 Erick,1 Abreu,2 5,3 0,4 0,5 0,6 10,7 25
0fff505d9,0 Michael,1 Abreu,2 28,3 0,4 0,5 2,6 2,7 24
0fff4ff13,0 Tony,1 Abreu,2 5,3 0,4 0,5 5,6 3,7 23

Each hex value needs to be changed (value A to value :). The reason I can not use a standard Search and replace program is because B is sometimes a future A, and after one line is changed I do not want it changed again.

I'm using an ini file for the change list

; IDchanges.ini
; Old Hex ID = New Hex ID

[ID]
0f844e070 = 0b8d68dba
0fc00b45a = 0fc00b45a
0fff4fd32 = 006cb96ac
0fff4fd33 = 0c628e255
0fff4fd34 = 00de04832
0fff4fd35 = 000d2b76a
0fff4fd36 = 0a3c19c92
0fff4fd37 = 0fff4fe05
0fff4fd38 = 00033d596
0fff4fd39 = 09b1c6b60
0fff4fd3a = 03cbc63dd
0fff4fd3b = 0703b1d30
0fff4fd3c = 09d4ab7fa
0fff4fd3d = 0a38dda84
......

Here is the autoit code I came up with

#include <file.au3>
$ini = IniReadSection ("IDchanges.ini" , "ID")
Dim $filelines
$filename = $CmdLine[1]
_FileReadToArray($filename,$filelines)
FileMove($filename,$filename & ".bak")
ProgressOn("Redoing HEX IDs", "Progress", "0 percent")
For $z = 1 To $filelines[0]
    $progresstemp = ($filelines[0] / 100)
    $progress = Round ($z / $progresstemp)
    ProgressSet( $progress, $progress & " percent")
    $i = 1
    For $i = 1 To $ini[0][0]
        If StringInStr ($filelines[$z] , $ini[$i][0]) Then
            $filelines[$z] = StringReplace ( $filelines[$z] , $ini[$i][0] , $ini[$i][1] )
            ExitLoop 1
        EndIf
    Next
Next            
ProgressSet(100 , "Done", "Complete")
sleep(2500)
ProgressOff()
_FileWriteFromArray($filename,$filelines,1)

I'm missing something because only a little more than half of the 3202 changes are made in the text files

Any ideas?

Do a MsgBox(0, "Test $filelines[0]", $filelines[0]) before the file move, see if that helps any.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

Posted Image

same results

Try...

MsgBox(0, "Test $filelines[0] and $ini[0][0]", "Lines (File):" & @TAB & $filelines[0] & @CRLF & "Lines (INI):" & @TAB & $ini[0][0])

Show me the results of that one.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

  • Developers

Your script looks good... can you post an example inputfile that will have failing records ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

reworked the script

I changed the INI file into a standard text file

0a7a0e91a,0a7a0e91a
0be0d7bad,0be0d7bad
020996a42,020996a42
094beb2e3,094beb2e3
060241d95,060241d95
0ee36e598,0ee36e598
03840a046,03840a046
....

_FileReadToArray the new INI/TXT file, then StringSplit each line of the text using "," as the delimiter. I then seach for 1st segment and replace with the 2nd segment of the StringSplit line.

It seams to work, but does anyone know why IniReadSection did not read every entry?

#include <file.au3>
Dim $filelines
Dim $inilines
$filename = $CmdLine[1]
_FileReadToArray($filename,$filelines)
_FileReadToArray("IDchanges.txt",$inilines)
MsgBox(0, "Test $filelines[0] and $ini[0][0]", "Lines (File):" & @TAB & $filelines[0] & @CRLF & "Lines (INI):" & @TAB & $inilines[0])
FileMove($filename,$filename & ".bak")
ProgressOn("Redoing HEX IDs", "Progress", "0 percent")
For $z = 1 To $filelines[0]
    $progresstemp = ($filelines[0] / 100)
    $progress = Round ($z / $progresstemp)
    ProgressSet( $progress, $progress & " percent")
    $i = 1
    For $i = 1 To $inilines[0]
        $inilinessplit = StringSplit ( $inilines[$i], ",")
        If StringInStr ($filelines[$z] , $inilinessplit[1]) Then
            $filelines[$z] = StringReplace ( $filelines[$z] , $inilinessplit[1] , $inilinessplit[2] )
            ExitLoop 1
        EndIf
    Next
Next            
ProgressSet(100 , "Done", "Complete")
sleep(2500)
ProgressOff()
_FileWriteFromArray($filename,$filelines,1)
Edited by krawhitham
Link to comment
Share on other sites

There may be an INI limit. I will check MSDN.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

INI Files seem to have a limit per section. I dont know if it is an AutoIt controlled item, or a Windows API item. I think it has to do with the Windows API, and approximately 64KB.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

  • Moderators

You can try some of these ini functions that Gary and I did to see if it corrects your problems.

Edit:

A link would be nice huh :)

http://www.autoitscript.com/forum/index.ph...c=32004&hl=

Edited by SmOke_N

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.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...