Jump to content
Tosyk

[resolved] find string duplicates and add Bip_, _XX to make them unique

Recommended Posts

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 by Tosyk

Share this post


Link to post
Share on other sites
$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 ... :huh2:

Edited by mikell

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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 functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites
#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 by Subz

Share this post


Link to post
Share on other sites
22 hours ago, mikell said:
..." & StringFormat('%02i', Assign(""iReplace"", Eval(""iReplace"")+1) * Eval(""iReplace""))"...

Hmmm. Possibly something simpler exists ... :huh2:

@mikell Can you explain how this works?  Don't understand how the count increments.

Share this post


Link to post
Share on other sites

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 :)


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 functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

@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 :idiot:
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 ' & '") & "'")

 

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...