Editing a text file inside PowerShell Script - powershell

I made a Powershell script to update my mpv scripts. One of them being this
https://raw.githubusercontent.com/mpv-player/mpv/master/player/lua/osc.lua
I want the script to edit and remove all instances of show_message(get_playlist(), 3) from the file.
I tried (get-content .\Scripts\osc.lua) | foreach-object {$_ -replace "show_message(get_playlist(), 3)", ""} | Out-File .\Scripts\osc.lua and (get-content .\portable_config\Scripts\osc.lua) | foreach-object {String.Replace [RegEx]::Escape('show_message(get_playlist(), 3)'),''} | Out-File .\portable_config\Scripts\osc.lua but they dont seem to work.
Basically I want the script to automatically download osc.lua(which works fine as intended) and then remove all instances of show_message(get_playlist(), 3) from the file. My PowerShell version is 5.1.

You can use the following with the Replace Operator:
$regex = [regex]::Escape('show_message(get_playlist(), 3)')
(Get-Content .\Scripts\osc.lua) -replace $regex |
Set-Content .\Scripts\osc.lua
Alternatively, you can use the String.Replace method from the String class, which does not use regex.
(Get-Content .\Scripts\osc.lua).Replace('show_message(get_playlist(), 3)','') |
Set-Content .\Scripts\osc.lua
Explanation:
When using the -replace operator, the matching mechanism is a regex match. Certain characters are metacharacters for regex and must be escaped before they can be interpreted literally. In this case ( and ) need to be escaped. You can either do that manually with \ (\( and \)) or use the Escape() method from the Regex class. From the code above, you can type $regex after its declaration and see how it was escaped.
When replacing a string with an empty string, you do not need to specify a replacement string. 'String' -replace 'ing' has the same results as 'String' -replace 'ing',''
Note: You are mixing Get-Content and Out-File in your code without using the -Encoding parameter. It is possible you could output a different encoding than the original file. If this matters to you, I'd suggest using the -Encoding parameter with the appropriate encoding.

Related

Replacing characters in the middle of a line in a text file with PowerShell

I have the code below that I use to replace a string within a text file and it works. However; I have run into the problem where the string I want to replace is not always exactly the same, but the first few characters are. I want to find the first few characters, count 3 characters ahead, and replace that with what I want.
For example, the line I am replacing may be 123xxx, or 123aaa, etc. but the value I am replacing it with is always going to be known. How would I go about replacing the 123xxx when I won't always know what the xxx is?
((Get-Content -path $ConfPath -Raw) -replace $OldVersion,$NewVersion) | Set-Content -Path $ConfPath
As -replace uses regex, you need to escape the characters that have special meaning like in your case the dot (any character in regex).
$OldVersion = 'jre1\.8\.0_\d{3}' # backslash escape the dots. \d{3} means 3 digits
$NewVersion = 'jre1.8.0_261' # this is just a string to replace with; not regex
((Get-Content -path $ConfPath -Raw) -replace $OldVersion,$NewVersion) | Set-Content -Path $ConfPath
I researched it and figured it out using regex. The code below does exactly what I wanted to do:
((Get-Content -path $ConfPath -Raw) -replace 'jre1.8.0_...',$NewVersion) | Set-Content -Path $ConfPath

Replace "special" Strings /---? in PowerShell

I already know how to replace simple characters or strings in powershell but now I have a headache with a "special" string.
For a normal string/text this line works perfectly fine.
(Get-Content $file.PSPath).Replace('lucky', 'dog') |
Set-Content c:\temp\test.txt
If I want to replace a string which contains a string/text like /---? it doesn't work.
(Get-Content $file.PSPath).Replace('de/---?lucky', 'dog') |
Set-Content c:\temp\test.txt
Also there is no error.

Replace newline with | [duplicate]

RegEx for Replace is kicking my butt. I am trying find:
value="COM8"/>
in a text file and replace "COM8" with another com port (ie "COM9", "COM13", etc).
(Get-Content 'C:\Path\File.config').Replace('/".*?"', '"COM99"') | Set-Content 'C:\Path\File.config'
Thank you in advance for any help you can provide.
Get-Content produces a list of strings. Replace() is called on each string via member enumeration. Meaning, you're calling the String.Replace() method, not the Regex.Replace() method. The former does only normal string replacements.
Use the -replace operator instead:
(Get-Content 'C:\Path\File.config') -replace '=".*?"', '="COM99"' |
Set-Content 'C:\Path\File.config'

Powershell .Replace RegEx

RegEx for Replace is kicking my butt. I am trying find:
value="COM8"/>
in a text file and replace "COM8" with another com port (ie "COM9", "COM13", etc).
(Get-Content 'C:\Path\File.config').Replace('/".*?"', '"COM99"') | Set-Content 'C:\Path\File.config'
Thank you in advance for any help you can provide.
Get-Content produces a list of strings. Replace() is called on each string via member enumeration. Meaning, you're calling the String.Replace() method, not the Regex.Replace() method. The former does only normal string replacements.
Use the -replace operator instead:
(Get-Content 'C:\Path\File.config') -replace '=".*?"', '="COM99"' |
Set-Content 'C:\Path\File.config'

Basic PowerShell Script Issue: "Expressions are only allowed as the first element of a pipeline"

I'm trying to write a simple script that reads a file, locates a string, replaces the string with another string, and stores all new file contents (with replaced string), in a new file. Here is what I'm using:
(Get-Content C:\file1.txt) | {$_ -replace "this:text", "withthis:text"} | Set-Content C:\file2.txt
The error I'm receiving is: "Expressions are only allowed as the first element of a pipeline"
I'm pretty sure this is because of the colon ":" character being in both the string I'm locating and replacing it with. I've tried escaping the colon character with "\" and "`" characters, but I'm receiving the same errors. Does anyone know what's wrong with this?
Thanks for the help.
The problem is the second element in your pipeline.
{$_ -replace "this:text", "withthis:text"}
This is a scriptblock (i.e. a piece of code). If you want to apply a scriptblock to all of the incoming items on a pipeline you can use the foreach-object cmdlet like this:
(Get-Content C:\file1.txt) | foreach-object {$_ -replace "this:text", "withthis:text"} | Set-Content C:\file2.txt
#shagun is using the % alias for the foreach-object cmdlet, so that code looks correct as well.
I guess it is because after first pipe you are not processing each result. so the right one will be according to me :
(Get-Content C:\file1.txt) | %{$_ -replace "this:text", "withthis:text"} | Set-Content C:\file2.txt