web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / replacing string in te...
Power Automate
Unanswered

replacing string in text file based on string in a second array

(0) ShareShare
ReportReport
Posted on by 251

Hi all,

Trying to match all instances of a string in a .txt file with a string in a second array without nested apply to each (because it times out)

 

I have a form where users can upload a text document and an excel list with a table of values, 1st column is the original string that appears in the text document, second column has the corresponding replacement string.

 

i'm using get file content using path to grab the text from the text file and then converting it from base64 to string in a compose:

 

base64ToString(outputs('Get_file_content_using_path')?['body']?['$content'])

output sample:

02290	C¶
02300	IF("XXX0300FZ" .EQ. ON) THEN GOTO 2312¶
02305	C¶
02310	ON("XXX030000")¶
02311	GOTO 2340¶
02312	OFF("XXX030000")¶
02315	C¶
02320	C	*** PREHEAT TEMP CONTROL ***¶
02330	C¶
02340	IF("XXX030000" .EQ. PRFON) THEN GOTO 2380¶
02350	SET(2.0,"XXX031400")¶
02360	LOOP(0,"XXX030300","XXX031500",75.0,2500,1,0,1,1.5,1.5,15.0,0)¶
02370	GOTO 2510¶
02380	IF("XXXOA0200" .GE. 65) THEN GOTO 2460¶
02390	IF($LOC12 .EQ. ON) THEN GOTO 2430¶
02400	SET(2.0,"XXX031500")¶
02410	LOOP(0,"XXX030300","XXX031400","XXX0303DA",300,1,300,1,7.0,2.0,14.0,0)¶
02420	GOTO 2510¶
02430	LOOP(0,"XXX030300","XXX031500","XXX0303DA",25,1,500,1,2.0,1.5,15.0,0)¶
02440	SET(2.0,"XXX031400")¶
02450	GOTO 2510¶
02460	SET(15.0,"XXX031400","XXX031500")¶
02470	C¶
02480	C¶

 

for the replacement string(s) i'm using list rows present in table, then doing a parse JSON and selecting the two values i want.
this gives me an array like this - called point names:

 

[
 {
 "Long": "XXX HVAC3.CLG VLV",
 "Short": "XXX031300"
 },
 {
 "Long": "XXX HVAC3.DISCH AIR CTRL",
 "Short": "XXX0303DA"
 },
 {
 "Long": "XXX HVAC3.DISCH HIGH LIMIT",
 "Short": "XXX0303DH"
 },
 {
 "Long": "XXX HVAC3.DISCH HUM",
 "Short": "XXX03D900"
 },
 {
 "Long": "XXX HVAC3.DISCH LOW LIMIT",
 "Short": "XXX0303DL"
 },
 {
 "Long": "XXX HVAC3.SUPPLY AIR FLOW",
 "Short": "XXX034200"
 }
]

 

with i am then appending to an array variable with an apply to each action.

 

ultimately, the goal is to look like this:


Now i'm stuck on how to actually do the replacement throughout the text document.

 

Ultimately the goal is to replace the "Short" string with the "Long" string:

 

02290	C¶
02300	IF("XXX HVAC3.LOW TEMP DETECTION" .EQ. ON) THEN GOTO 2312¶
02305	C¶
02310	ON("XXX HVAC3.SFAN A S/S")¶
02311	GOTO 2340¶
02312	OFF("XXX HVAC3.SFAN A S/S")¶
02315	C¶
02320	C	*** PREHEAT TEMP CONTROL ***¶
02330	C¶
02340	IF("XXX HVAC3.SFAN A S/S" .EQ. PRFON) THEN GOTO 2380¶
02350	SET(2.0,"XXX HVAC3.FB DAMPER")¶
02360	LOOP(0,"XXX HVAC3.DISCH TEMP","XXX HVAC3.HTG VLV",75.0,2500,1,0,1,1.5,1.5,15.0,0)¶
02370	GOTO 2510¶
02380	IF("XXX OUTSIDE AIR" .GE. 65) THEN GOTO 2460¶
02390	IF($LOC12 .EQ. ON) THEN GOTO 2430¶
02400	SET(2.0,"XXX HVAC3.HTG VLV")¶
02410	LOOP(0,"XXX HVAC3.DISCH TEMP","XXX HVAC3.FB DAMPER","XXX HVAC3.DISCH AIR CTRL",300,1,300,1,7.0,2.0,14.0,0)¶
02420	GOTO 2510¶
02430	LOOP(0,"XXX HVAC3.DISCH TEMP","XXX HVAC3.HTG VLV","XXX HVAC3.DISCH AIR CTRL",25,1,500,1,2.0,1.5,15.0,0)¶
02440	SET(2.0,"XXX HVAC3.FB DAMPER")¶
02450	GOTO 2510¶
02460	SET(15.0,"XXX HVAC3.FB DAMPER","XXX HVAC3.HTG VLV")¶
02470	C¶
02480	C¶

 

 

I originally had a macro to do this, but, the organization that i work for has locked down macros significantly. I wanted to try and execute the script in word using office scripts in power automate, but, it appears that is only a feature in excel.

So far, i've tried an apply to each for the array "point names" and then do a replace on the text of the document, which works fine, but, each iteration only replaces the one value pair and i don't know how to "save" the changes so that as it iterates ALL points in the point array will be replaced into a single document.

 

I tried putting the text into a sharepoint list and using an update item action, but, that times out with only 140 lines of text and 16 string pairs in the points array., and usually there will be thousands of lines of text and hundreds of points in the points array. 

 

I've also tried putting each line of the text document into an array and updating the item when there is a match using a conditional, which also works, but times out if there is over a few dozen lines.

 

I also thought of using filter array with apply to each - but, if i use the text array as the input for the apply to each then i don't have a way to iterate through the selected point name, i just end up comparing the entire array output from the select.

 

Am i missing some kind of obvious fix here (besides just going back to the macro, our IT has been adamant that isn't going to be an option)

is there a better way?

Categories:
I have the same question (0)
  • Verified answer
    Nived_Nambiar Profile Picture
    18,129 Super User 2025 Season 2 on at

    Hi @Seuadr 

     

    Give a try this way.

     

    for demo i have saved text data and array of replacements in array 

     

    Step 1: initialize a temp_text variable 

    Nived_Nambiar_0-1680799093113.png

     

     

    Step 2 [ demo]: store the text details in text variable

    Nived_Nambiar_1-1680799124016.png

     

     

    Step 3 [demo]: store the array in arr variable

    Nived_Nambiar_2-1680799151790.png

     

    Step 4: Loop through arr1 variable and then use replace action to replace the short with long on text data like below and assign it to temp_text variable

     

    Nived_Nambiar_3-1680799238578.png

     

    expression: 

    replace(variables('text'),items('Apply_to_each')?['Short'],items('Apply_to_each')?['Long'])
     
    Step 5: assign temp_text to text variable
    Nived_Nambiar_4-1680799286088.png

     

     

    Step 6: out of loop use compose action to see the updated value of text variable 🙂

     

    Hope this idea helps 

  • Seuadr Profile Picture
    251 on at

    Nived N,

     

    This worked perfectly! thank you!

    I suspected somewhere along the way i was overcomplicating it, but being rather inexperienced it can be pretty easy to get carried away down the wrong path!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 538 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 405 Moderator

#3
abm abm Profile Picture

abm abm 252 Most Valuable Professional

Last 30 days Overall leaderboard