Tosyk Posted July 3, 2020 Posted July 3, 2020 (edited) I have a text file: version 1 nodes 0 "Bip_Pelvis" -1 <...> 240 "1FC8040F" 239 241 "00000000" 0 242 "00000000" 0 243 "00000000" 0 244 "00000000" 0 245 "00000000" 0 246 "00000000" 0 247 "00000000" 0 248 "00000000" 0 249 "00000000" 0 250 "00000000" 0 251 "00000000" 0 252 "00000000" 0 253 "00000000" 0 254 "00000000" 0 255 "00000000" 0 256 "Bip_BackWind_Helper" 11 257 "Bip_UpperArmTwist_R" 13 258 "Bip_UpperArmTwist_L" 17 <...> and I want to add an unique prefix and suffix to each of 00000000. to make this file be something like: version 1 nodes 0 "Bip_Pelvis" -1 <...> 240 "1FC8040F" 239 241 "Bip_00000000_01" 0 242 "Bip_00000000_02" 0 243 "Bip_00000000_03" 0 244 "Bip_00000000_04" 0 245 "Bip_00000000_05" 0 246 "Bip_00000000_06" 0 247 "Bip_00000000_07" 0 248 "Bip_00000000_08" 0 249 "Bip_00000000_09" 0 250 "Bip_00000000_10" 0 251 "Bip_00000000_11" 0 252 "Bip_00000000_12" 0 253 "Bip_00000000_13" 0 254 "Bip_00000000_14" 0 255 "Bip_00000000_15" 0 256 "Bip_BackWind_Helper" 11 257 "Bip_UpperArmTwist_R" 13 258 "Bip_UpperArmTwist_L" 17 <...> how I can perform this? Edited July 4, 2020 by Tosyk
mikell Posted July 3, 2020 Posted July 3, 2020 (edited) $txt = "version 1" & @crlf & _ "nodes" & @crlf & _ "0 ""Bip_Pelvis"" -1" & @crlf & _ @crlf & _ "<...>" & @crlf & _ @crlf & _ "240 ""1FC8040F"" 239" & @crlf & _ "241 ""00000000"" 0" & @crlf & _ "242 ""00000000"" 0" & @crlf & _ "243 ""00000000"" 0" & @crlf & _ "244 ""00000000"" 0" & @crlf & _ "245 ""00000000"" 0" & @crlf & _ "246 ""00000000"" 0" & @crlf & _ "247 ""00000000"" 0" & @crlf & _ "248 ""00000000"" 0" & @crlf & _ "249 ""00000000"" 0" & @crlf & _ "250 ""00000000"" 0" & @crlf & _ "251 ""00000000"" 0" & @crlf & _ "252 ""00000000"" 0" & @crlf & _ "253 ""00000000"" 0" & @crlf & _ "254 ""00000000"" 0" & @crlf & _ "255 ""00000000"" 0" & @crlf & _ "256 ""Bip_BackWind_Helper"" 11" & @crlf & _ "257 ""Bip_UpperArmTwist_R"" 13" & @crlf & _ "258 ""Bip_UpperArmTwist_L"" 17" & @crlf & _ @crlf & _ "<...>" ; Msgbox(0,"", $txt) Local $iReplace = 0, $sOutput = "'" & StringReplace($txt, "'", "''") & "'" $sOutput = Execute ( StringRegExpReplace($sOutput, "(00000000)("")", _ "' & 'Bip_$1' & '_'" & _ " & StringFormat('%02i', Assign(""iReplace"", Eval(""iReplace"")+1) * Eval(""iReplace""))" & _ " & '$2 ' & '") ) MsgBox(0, "", $sOutput) Hmmm. Possibly something simpler exists ... Edited July 3, 2020 by mikell Subz 1
Tosyk Posted July 3, 2020 Author Posted July 3, 2020 (edited) oh my, this is heavy thank you! I'll try this out Edited July 3, 2020 by Tosyk
Tosyk Posted July 3, 2020 Author Posted July 3, 2020 how to feed .txt file to that script and not the text itself. I mean I need to automatic find these strings (00000000) in any TXT file and replace them with rules above
seadoggie01 Posted July 3, 2020 Posted July 3, 2020 I would take a look at the help file. Try a couple things, FileRead will help you here. All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types
Tosyk Posted July 3, 2020 Author Posted July 3, 2020 I've tried several approaches but can't get anything useful :/ that's why I came here
Subz Posted July 4, 2020 Posted July 4, 2020 RegExpr isn't my forte but something like this should work: Local $sFileRead = FileRead(@ScriptDir & "\Demo.txt") Local $sFileData = StringRegExpReplace($sFileRead, '([0-9]{3} \")([0]{8})', "$1Bip_$2") ConsoleWrite($sFileData & @CRLF)
Tosyk Posted July 4, 2020 Author Posted July 4, 2020 @Subz this is working, thanks! but how to add suffix as well and save the file to same location?
Subz Posted July 4, 2020 Posted July 4, 2020 (edited) #include <File.au3> Local $sFileName = @ScriptDir & "\Demo.txt" Local $aFileData _FileReadToArray($sFileName, $aFileData) Local $iCount = 1 For $i = 1 To $aFileData[0] $aFileData[$i] = StringRegExpReplace($aFileData[$i], '([0-9]{3} \")([0]{8})(["]{1})', "$1Bip_$2_" & StringFormat("%02d", $iCount) & "$3") If @extended Then $iCount += 1 Next Local $hFileOpen = FileOpen(@ScriptDir & "\Updated.txt", 2) FileWrite($hFileOpen, _ArrayToString($aFileData, @CRLF, 1)) FileClose($hFileOpen) ShellExecute(@ScriptDir & "\Updated.txt") Edited July 4, 2020 by Subz
Tosyk Posted July 4, 2020 Author Posted July 4, 2020 this is so great! thank you! always learn something
Subz Posted July 4, 2020 Posted July 4, 2020 22 hours ago, mikell said: ..." & StringFormat('%02i', Assign(""iReplace"", Eval(""iReplace"")+1) * Eval(""iReplace""))"... Hmmm. Possibly something simpler exists ... @mikell Can you explain how this works? Don't understand how the count increments.
seadoggie01 Posted July 4, 2020 Posted July 4, 2020 Basically, Assign(iReplace, Eval(iReplace) + 1) is the same thing as $iReplace += 1. Assign returns 1 (Success) which, when multiplied, doesn't affect the final value returned by the last Eval(iReplace). Mikell could've just as easily moved Assign("iReplace", Eval("iReplace") + 1) to the following line to make this more readable, but I think he was going for a single line solution Subz 1 All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types
mikell Posted July 5, 2020 Posted July 5, 2020 @Subz@seadoggie01 is absolutely right The analysis is correct. "Assign(iReplace, Eval(iReplace) + 1)" does the $iReplace incrementation (returns 1) so the variable can be used in the StringFormat (and the regex) using "Eval(iReplace)" which returns the value ... and yes, the challenge (for fun) was to build a one-liner - not totally achieved since the "$iReplace = 0 " declaration is needed first I formerly discovered this concept in a script from @jguinch (credits to you my friend !) I was in a hurry, this one is cleaner - more readable Local $iReplace = 0 $sOutput = Execute ("'" & StringRegExpReplace($txt, "(00000000)("")", _ "' & 'Bip_$1_'" & _ " & StringFormat('%02i', Assign('iReplace', Eval('iReplace')+1) * Eval('iReplace'))" & _ " & '$2 ' & '") & "'") Subz 1
jguinch Posted July 6, 2020 Posted July 6, 2020 nice 😄 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
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