Replace single strings in file with single lines from another file

I’m trying to replace a string of 9 numbers in an HTML file with HTML-formatted hyperlinks in another text file - so, iterate through the string matches in the first HTML file and replace them with entire single lines from the text file.
The HTML file has Salesforce ticket numbers, and the text file has HTML-formatted hyperlinks that I generated through a process… so I’m trying to automate inserting the hyperlinks into the HTML file.

Thank you!

Chilly,
welcome to the forum.

You know you didn’t even ask a question, don’t you? :wink:

Regardless of that - what have you tried so far? Please share your code and explain what’s not working as expected or what particular issue you have.

What you ask for is a very common task. There are literally thousands of examples out there for tasks like this. Did you try to search for a solution first? Try StackOverflow or Reddit or PowerShellGallery or - of course - here in this forum.

1 Like

*Yes, I’ve been trying to accomplish this for a month…

How To Replace Single Strings In One File With Single Lines In Another File?

  • File 1 is an HTML file, and in that file are nine-digit ticket numbers (I parse those with \b(\d\d\d\d\d\d\d\d\d)).

  • File 2 is a series of HTML-formatted hyperlink lines that correspond with the ticket numbers in File 1.
    - <a href="https://commpany.my.salesforce.com/4444v88001ajpls;"style=“color:#0070C0;background:#DDEBF7;font-size:12pt;text-decoration:underline;“target=”_blank;”>123456789

(For some reason the closing “a” tag isn’t showing, but it’s there.)

This one sortof does the job, but it just keeps looping through infinitely:

$urls = Get-Content ‘C:\MY_FILE_PATH\HTML_Formatted_URL_Hyperlinks.txt’

$html = Get-Content ‘C:\MY_FILE_PATH\sheet001.htm’

ForEach ($i in $urls) {
For ($i=0; $i -lt $html.Length; $i++)
{
$urlsadded = $html -replace ‘\b(\d\d\d\d\d\d\d\d\d)’, $i
$urlsadded | Out-File -FilePath ‘C:\MY_FILE_PATH\sheet001.htm’
}
}

This one just replaces the ticket numbers in File 1 with the file path for File 2:

$urls = Get-Content ‘C:\MY_FILE_PATH\HTML_Formatted_URL_Hyperlinks.txt’

$html = Get-Content ‘C:\MY_FILE_PATH\orig_sheet.htm’

foreach ($line in $urls) {
$urlsadded = $html -replace ‘\b(\d\d\d\d\d\d\d\d\d)’, $i
$urlsadded | Out-File ‘C:\MY_FILE_PATH\sheet001.htm’ -Append
}

I think I’m close, but I don’t have enough experience with PowerShell to understand how to prevent it from infinitely looping, and I don’t understand why it’s inserting the file path instead of the text inside the file.

Chilly,
before we proceed … could you please format your code as code using the “preformatted Text” button ( </> )?
Thanks in advance.

(Please edit your existing post to correct the formatting. Do not create a new one.)