HezzelQuartz Posted yesterday at 05:57 AM Share Posted yesterday at 05:57 AM If I have a txt file containing text below X0 00112233 X1 AABBCCDD X2 22446688 X3 BBDDFF00 X4 11335577 X5 AACCEEFF X6 11AA22BB X7 CC33DD44 X8 55EE66FF X9 ABCD9988 X10 8800AADD X11 EEFF3344 X12 6622AAEE X13 5599CCFF X14 AAFF2200 X15 0099AACC Then, I want to change it to be below X0 00112033 X1 AABBC1DD X2 22446288 X3 BBDDF300 X4 11335477 X5 AACCE5FF X6 11AA26BB X7 CC33D744 X8 55EE68FF X9 ABCD9988 X10 8800AADD X11 EEFF3B44 X12 6622ACEE X13 5599CDFF X14 AAFF2E00 X15 0099AFCC So I want to change the third hex character from right incrementally like these: 1st line: 0 2nd Line: 1 15th line: F 16th line: 0 (back to 0) How can I do that? I try to create script like this: $text = FileRead(@ScriptDir & "\" & "example.txt") $hex = StringRegExpReplace($text, 'X\d+\h[[:xdigit:]]{5}\K([[:xdigit:]]{1})', 'F') FileWrite($locscript & "newexample.txt", $hex) I just confused what to do to change the replaced string in every line Thank You Sorry for bad explanation Link to comment Share on other sites More sharing options...
ioa747 Posted yesterday at 06:48 AM Share Posted yesterday at 06:48 AM (edited) old school without regexp #include <Array.au3> Local $text = FileRead(@ScriptDir & "\example.txt") Local $aList = StringSplit($text, @CRLF, 1) ;~ _ArrayDisplay($aList) Local $Newtext Local $idx = 16 For $i = 1 To $aList[0] ConsoleWrite($i & ") " & $aList[$i] & " -> ") Local $front = StringTrimRight($aList[$i], 3) Local $back = Hex($idx, 1) & StringRight($aList[$i], 2) ConsoleWrite($front & $back & @CRLF) $Newtext &= $front & $back & @CRLF $idx += 1 If $idx = 17 Then $idx = 1 Next $Newtext = StringTrimRight($Newtext, 2) $newexample = @ScriptDir & "\newexample.txt" If FileExists($newexample) Then FileDelete($newexample) FileWrite($newexample, $Newtext) Edited yesterday at 07:02 AM by ioa747 correction I know that I know nothing Link to comment Share on other sites More sharing options...
jchd Posted yesterday at 08:34 AM Share Posted yesterday at 08:34 AM Regex school: Local $text = FileRead(@ScriptDir & "\" & "source.txt") Local $hex = Execute('"' & StringRegExpReplace($text, '(?m)(?<=[[:xdigit:]]{5})([[:xdigit:]])(?=[[:xdigit:]]{2})', '" & _IncHex() & "') & '"') ConsoleWrite($hex) Func _IncHex() Local Static $Inc = 15 $Inc += 1 Return Hex(Mod($Inc, 16), 1) EndFunc I added two extra line in the source file, yielding: X0 00112033 X1 AABBC1DD X2 22446288 X3 BBDDF300 X4 11335477 X5 AACCE5FF X6 11AA26BB X7 CC33D744 X8 55EE68FF X9 ABCD9988 X10 8800AADD X11 EEFF3B44 X12 6622ACEE X13 5599CDFF X14 AAFF2E00 X15 0099AFCC X16 FD5A00B6 X17 10E51194 ioa747 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 hereRegExp tutorial: enough to get startedPCRE 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) Link to comment Share on other sites More sharing options...
HezzelQuartz Posted yesterday at 11:31 AM Author Share Posted yesterday at 11:31 AM 4 hours ago, ioa747 said: old school without regexp #include <Array.au3> Local $text = FileRead(@ScriptDir & "\example.txt") Local $aList = StringSplit($text, @CRLF, 1) ;~ _ArrayDisplay($aList) Local $Newtext Local $idx = 16 For $i = 1 To $aList[0] ConsoleWrite($i & ") " & $aList[$i] & " -> ") Local $front = StringTrimRight($aList[$i], 3) Local $back = Hex($idx, 1) & StringRight($aList[$i], 2) ConsoleWrite($front & $back & @CRLF) $Newtext &= $front & $back & @CRLF $idx += 1 If $idx = 17 Then $idx = 1 Next $Newtext = StringTrimRight($Newtext, 2) $newexample = @ScriptDir & "\newexample.txt" If FileExists($newexample) Then FileDelete($newexample) FileWrite($newexample, $Newtext) Thank You.. I tried and I think it works well ioa747 1 Link to comment Share on other sites More sharing options...
HezzelQuartz Posted yesterday at 11:32 AM Author Share Posted yesterday at 11:32 AM 2 hours ago, jchd said: Regex school: Local $text = FileRead(@ScriptDir & "\" & "source.txt") Local $hex = Execute('"' & StringRegExpReplace($text, '(?m)(?<=[[:xdigit:]]{5})([[:xdigit:]])(?=[[:xdigit:]]{2})', '" & _IncHex() & "') & '"') ConsoleWrite($hex) Func _IncHex() Local Static $Inc = 15 $Inc += 1 Return Hex(Mod($Inc, 16), 1) EndFunc I added two extra line in the source file, yielding: X0 00112033 X1 AABBC1DD X2 22446288 X3 BBDDF300 X4 11335477 X5 AACCE5FF X6 11AA26BB X7 CC33D744 X8 55EE68FF X9 ABCD9988 X10 8800AADD X11 EEFF3B44 X12 6622ACEE X13 5599CDFF X14 AAFF2E00 X15 0099AFCC X16 FD5A00B6 X17 10E51194 Thank You I tried and I think it works well But Could you please explain what is the function of execute? I read in the help file but I'm still confused... I just saw execute: "Execute an expression." Thank You Link to comment Share on other sites More sharing options...
jchd Posted yesterday at 12:23 PM Share Posted yesterday at 12:23 PM The string supplied to Execute is AutoIt code, which is then executed. This is the way to execute dynamically created code. 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 hereRegExp tutorial: enough to get startedPCRE 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) Link to comment Share on other sites More sharing options...
Nine Posted yesterday at 01:40 PM Share Posted yesterday at 01:40 PM Or from the hybrid school : #include <Array.au3> $aText = FileReadToArray("Test.txt") For $i = 0 To UBound($aText) - 1 $aText[$i] = StringRegExpReplace($aText[$i], "(?<=[[:xdigit:]]{5})([[:xdigit:]])(?=[[:xdigit:]]{2})", Hex(Mod(Number(StringRegExp($aText[$i], "X(\d+)", 1)[0]), 16), 1)) Next ConsoleWrite(_ArrayToString($aText, @CRLF) & @CRLF) ; or _FileWriteFromArray ioa747 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
ioa747 Posted yesterday at 02:26 PM Share Posted yesterday at 02:26 PM (edited) I found the Mod() function very useful old school with hybrid influences #include <Array.au3> $aList = FileReadToArray("example.txt") For $i = 0 To UBound($aList) - 1 $aList[$i] = StringTrimRight($aList[$i], 3) & Hex(Mod($i + 16, 16), 1) & StringRight($aList[$i], 2) Next ConsoleWrite(_ArrayToString($aList, @CRLF) & @CRLF) ; or _FileWriteFromArray Edited yesterday at 06:33 PM by ioa747 I know that I know nothing Link to comment Share on other sites More sharing options...
Werty Posted yesterday at 06:27 PM Share Posted yesterday at 06:27 PM (edited) From the simple school: FileCopy("example.txt", "newexample.txt", 1) $file = FileOpen("newexample.txt", 1 + 16) FileSetpos($file, 9, 0) For $loop = 0 To 15 FileWrite($file, Hex($loop, 1)) FileSetpos($file, 13, 1) Next FileClose($file) Edited 23 hours ago by Werty added filecopy Some guy's script + some other guy's script = my script! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now