Jump to content

help replacing string in string


Recommended Posts

Hello,

i have a txt file filled with lines like this https://pastebin.com/raw/PvwFc1Wg

And i would like, for each line starting with  the command MoveJ or MoveL, replace the third value in the first Brackets with its value - 3
So in this example i have to replace in the first line 227.68 with 224.68 ( which is 227.68 - 3) and so on up to the end of my file.

How can i do that ? i know how to read and write the lines in the TXT file, what i miss is how to get the value and change it in the line.

 

Thank you for your help

Edited by cetipabo
Link to comment
Share on other sites

You might also use a regular expression to do the job in one shot  :)

$txt = "    MoveJ [[-320.62,-132.78,227.68],[0.998219,0.0444207,-0.0168298,0.0360765],[-1,-1,-1,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]],v300,fine,Out_Ciseau\WObj:=rep_refil;" & @crlf & _ 
    "    Set S_lames;" & @crlf & _ 
    "    MoveL [[-257.19,-178.26,187.01],[0.952349,-0.0379082,-0.0440127,0.299429],[0,0,-1,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]],v300,fine,Out_Ciseau\WObj:=rep_refil;" & @crlf & _ 
    "    coupe_G;" & @crlf & _ 
    "    SetAO Effort,30 * Coeff_JE;" & @crlf & _ 
    "    MoveL [[-262.75,-132.35,192.71],[0.053336,-0.998395,-0.009298,-0.016629],[0,-1,0,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]],V_approche,z10,refilat\WObj:=rep_refil;" & @crlf & _ 
    "    SetAO Effort,30 * Coeff_JE;" & @crlf & _ 
    "    MoveL [[-256.91,-165.70,186.64],[0.0536583,-0.976169,-0.206382,-0.0402727],[0,-1,-1,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]],V_rapide,Z_refil,refilat\WObj:=rep_refil;" & @crlf & _ 
    "    MoveL [[-232.36,-161.50,188.68],[0.0352275,-0.978176,-0.201269,-0.0377103],[0,-1,-1,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]], V_rapide, Z_Refil, refilat\WObj:=rep_refil;"

$s = Execute("'" & StringRegExpReplace($txt, "(?m)^\h*Move[JL]\h\[\[(?:[\d.-]+,){2}\K(\d+)", "' & $1-3 & '") & "'") 
Msgbox(0,"", $s)

Edit : comments

(?m) : multiline option
^ : start of line
\h*Move[JL]\h\[\[(?:[\d.-]+,){2} : describe the beginning of the concerned line (in regex syntax )
\K : but don't touch it !
(\d+) : the integer part of the number to match

 

 

Edited by mikell
Link to comment
Share on other sites

Hello !

Sorry for the delay, and thank you for your help. I'll try your suggestions.

 

EDIT:

Thanks @Zednaand @mikell
@mikell's example in one-line seduced me ! it works as expected, but it wouldn't work if one day i decide to substract a decimal number like 2.55 for example.

Well, so i tried to adapt your regex in order to be able to do the same for the 1st and the 2nd values in the bracket...and no way, that's really a hard, too complicated Regex for me 😪
i thought that replacing {2} with {1} would capture the second value, but it doesn't, and for the first value we can't do it based on the comma position, so i'm stuck here too 😬
Could you tell me what would be the regex in these case ? and if possible working with decimal numbers ?

Thanks again for your help 

Edited by cetipabo
Link to comment
Share on other sites

4 hours ago, cetipabo said:

to adapt your regex in order to be able to do the same for the 1st and the 2nd values in the bracket

What probably caused you trouble is the hyphen, which is present in the 1st and 2nd numbers but not in the third

; substract 3 from the first one
$s = Execute("'" & StringRegExpReplace($txt, "(?m)^\h*Move[JL]\h\[\[-?\K(\d+)", "' & $1-3 & '") & "'") 

; substract 3 from the 2nd one
$s = Execute("'" & StringRegExpReplace($txt, "(?m)^\h*Move[JL]\h\[\[(?:[\d.-]+,)-?\K(\d+)", "' & $1-3 & '") & "'")

Please pay attention to the " -? " which means "one hyphen, optional"
Here for the 2nd one you get -132.78 > -129.78

If you put the optional hyphen inside the capturing group

; substract 3 from the 2nd one
$s = Execute("'" & StringRegExpReplace($txt, "(?m)^\h*Move[JL]\h\[\[(?:[\d.-]+,)\K(-?\d+)", "' & $1-3 & '") & "'")

Then you get -132.78 > -135.78
 

4 hours ago, cetipabo said:

if one day i decide to substract a decimal number like 2.55 for example

Use the optional hyphen too but in this case you don't want to operate on the integer part of the number, you want to operate on the whole number (including decimals), so instead of (\d+) you have to use (\d+\.\d+) "digits/dot/digits"

; substract 2.55 from the 3rd one
$s = Execute("'" & StringRegExpReplace($txt, "(?m)^\h*Move[JL]\h\[\[(?:[\d.-]+,){2}\K(\d+\.\d+)", "' & $1-2.55 & '") & "'")

BTW for versatility, if the number may be an integer with no dot/decimals you can use (\d+\.?\d*)

Regex looks like klingon but is not so difficult as long as you understand the syntax and apply it strictly  :)

Edited by mikell
Link to comment
Share on other sites

On 10/7/2021 at 12:18 PM, Zedna said:

Use StringInStr() to get position of your brackets and then use StringMid() to get left/mid/right piece of string at that position ...

Here it is:

$txt = "    MoveJ [[-320.62,-132.78,227.68],[0.998219,0.0444207,-0.0168298,0.0360765],[-1,-1,-1,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]],v300,fine,Out_Ciseau\WObj:=rep_refil;" & @crlf & _
    "    Set S_lames;" & @crlf & _
    "    MoveL [[-257.19,-178.26,187.01],[0.952349,-0.0379082,-0.0440127,0.299429],[0,0,-1,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]],v300,fine,Out_Ciseau\WObj:=rep_refil;" & @crlf & _
    "    coupe_G;" & @crlf & _
    "    SetAO Effort,30 * Coeff_JE;" & @crlf & _
    "    MoveL [[-262.75,-132.35,192.71],[0.053336,-0.998395,-0.009298,-0.016629],[0,-1,0,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]],V_approche,z10,refilat\WObj:=rep_refil;" & @crlf & _
    "    SetAO Effort,30 * Coeff_JE;" & @crlf & _
    "    MoveL [[-256.91,-165.70,186.64],[0.0536583,-0.976169,-0.206382,-0.0402727],[0,-1,-1,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]],V_rapide,Z_refil,refilat\WObj:=rep_refil;" & @crlf & _
    "    MoveL [[-232.36,-161.50,188.68],[0.0352275,-0.978176,-0.201269,-0.0377103],[0,-1,-1,0],[9E+09,9E+09,9E+09,9E+09,9E+09,9E+09]], V_rapide, Z_Refil, refilat\WObj:=rep_refil;"

$txt = StringSplit($txt, @CRLF, 1)
$text_out = ''

For $i = 1 To $txt[0]
    $line = $txt[$i]
    $j1 = StringInStr($line, '[[', 1)
    If $j1 > 0 Then
        $j2 = StringInStr($line, ',', 1, 2, $j1+2) ; second comma after [[
        $j3 = StringInStr($line, ']', 1, 1, $j2+1) ; first ] after comma

        $number = StringMid($line, $j2+1, $j3-$j2-1)
        $line = StringLeft($line,$j2) & $number-3 & StringMid($line,$j3)
    EndIf
    $text_out &= $line & @CRLF
Next

ConsoleWrite($text_out)

 

EDIT: slightly simplified, removed unncessary If/Then

Edited by Zedna
Link to comment
Share on other sites

9 hours ago, mikell said:

Regex looks like klingon but is not so difficult as long as you understand the syntax and apply it strictly

This. But sometimes it still looks like klingon so you put it in https://regex101.com/ to see what it means and test it

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
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Thanks @mikell for your explanations ! Now my project is fully working.

this is how it looks :
rHdhWca.png

The Fisrt, Second and third values in the bracket are the 3 axis position of an industrial robot that removes the excess leather from the bottom of a shoe during its manufacture before sticking the sole.
Sometimes we need to quickly shift the X, Y or Z position of the bot, and it is way faster to do it with an automated script than having to do it from the bot interface.

So i created in my GUI the 3 Offsets X Y Z that I might want to add in the file in order to shift the bot path.

So here is my code:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Change bot path", 438, 177, 192, 124)
$Input1 = GUICtrlCreateInput("", 16, 24, 313, 21)
$Button1 = GUICtrlCreateButton("File", 336, 24, 75, 21)
$Button2 = GUICtrlCreateButton("Modify", 16, 120, 75, 25)
$Label1 = GUICtrlCreateLabel("Offset Z", 144, 58, 40, 15)
$Label2 = GUICtrlCreateLabel("Offset X", 16, 58, 42, 15)
$Label3 = GUICtrlCreateLabel("Offset Y", 80, 58, 42, 15)
$Input2 = GUICtrlCreateInput("0", 16, 74, 41, 21)
$Input3 = GUICtrlCreateInput("0", 80, 74, 41, 21)
$Input4 = GUICtrlCreateInput("0", 144, 74, 41, 21)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
 $nMsg = GUIGetMsg()
 Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit

    case $Button1
        Local $sFileOpenDialog = FileOpenDialog("Select bot path file", @scriptdir & "\", "File (*.mod)")
        
        GUICtrlSetData ($Input1, $sFileOpenDialog)

     case $Button2
        $sFilePath = GUICtrlRead($Input1)
        $offsetX = GUICtrlRead($Input2)
        $offsetY = GUICtrlRead($Input3)
        $offsetZ = GUICtrlRead($Input4)

        Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
        Local $txt = FileRead($hFileOpen)

        ; we replace the values with the new ones by adding/decreasing the value of the offsets
        ; https://www.autoitscript.com/forum/topic/206794-help-replacing-string-in-string/?tab=comments#comment-1490540
        $newtxt = Execute("'" & StringRegExpReplace($txt, "(?m)^\h*Move[JL]\h\[\[\K(-?\d+\.\d+)", "' & $1+$offsetX & '") & "'")
        $newtxt = Execute("'" & StringRegExpReplace($newtxt, "(?m)^\h*Move[JL]\h\[\[(?:[\d.-]+,)\K(-?\d+\.\d+)", "' & $1+$offsetY & '") & "'")
        $newtxt = Execute("'" & StringRegExpReplace($newtxt, "(?m)^\h*Move[JL]\h\[\[(?:[\d.-]+,){2}\K(-?\d+\.\d+)", "' & $1+$offsetZ & '") & "'")

        ;we write the new file
        $newname = StringReplace($sFilePath,".mod","_new.mod")
        Local $hNewFileOpen = FileOpen($newname, $FO_OVERWRITE)
        FileWrite ($hNewFileOpen, $newtxt)

        FileClose($hFileOpen)
        FileClose($hNewFileOpen)

        msgbox(0,"","COMPLETED")

 EndSwitch
WEnd

And i joined an example for testing

pathfile.zip

Edited by cetipabo
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...