How to remove from character x to character y in a text file

Hello everybody,

I’, trying to make a script, and I’ve become stuck at a part that requires me to remove everything from character x to character y, and I cant seem to find anything on the subject.

I’ve tried using -replace, but the file I’m working with might contain the characters I’m trying to get rid of other places in the text, and I dont want those removed.
Putting the text into a variable and using substrings doesn’t do me any good here either, or I’m just using it wrong, since I cant seem to get the parameters right.

Any help on how to get this written is much appreciated!

Morten,
Welcome to the forum. :wave:t4:

Unfortunately you did not provide enough information to advice something meaningful.

Please share the code you’ve got stuck with, along with some sample input data and the expected output. Everything as plain text formatted as code, please.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Hello Olaf!

The part of the code I’m stuck with is this;

(
    (Get-Content "MortenTest.bat" -Raw
    ) -replace '6|7|8|9|10|11|12|13|14', $nyKundeVersion
) | Set-Content "MortenTest.bat"
(
    (Get-Content "MortenTest.bat" -Raw
    ) -replace ($arrayBatch.Substring($substring,6),"")

Some of the path names has been removed for privacy reasons.

The version will be something like 10.0.12345, and so far this will replace it with something like 10.0.54321.0.12345. I’m trying to have 0.12345 removed every time. The number it will start on might vary, but that has been taken into account. Numbers from 6 and up is not “hardcoded” its not anywhere in the text I’m working with.

Sorry that I cant share any of the actual files im working with, it has sensitive information. :slight_smile:

So it is about version numbers?! :smirk:

So you should obfuscate sensitive information and replace them with random text. The information you shared so far are still pretty insufficient.

And you don’t have to share a complete file content when the relevant part is just a few lines. :wink:

Since the replace operator works with regex this line of code would replace each single occurence of each found number with the content of the variable $nyKundeVersion. I wouldn’t expect you to really want this. :wink:

Since the replace operator works with regex I wouldn’t expect that to be a valid regex pattern. :wink:

For such a common use case like version numbers you don’t have to re-invent the wheel again. Simply search for “regex version number” and you will surely find more than enough examples. Here you have some:

And here you have a good starting point for general information about regex:

“It’s always the damn version numbers!” :smiley:

I will get you the part of the .bat that has the version number, the rest doesn’t really have anything to do with this post;

SET /p VERSION=<c:\Folder\VersionTXT\Success.version.txt
SET /A VERSION=%VERSION%+1
>c:\Folder\VersionTXT\Success.version.txt ECHO %VERSION%
SET VERSION=10.0.33750.%VERSION%
ECHO %VERSION%

The version.text just has a number regarding the amount of updates the package here has recieved.

Most of the numbers in the .bat are hidden away, only the %VERSION%+1 and the version number is not in a variable, so we can make some drastic things when it comes to changing the. 6…14 is not any place in the .bat, so its fine.

I just wrote something that looked like it, this is the last attempt I made before reaching out after some help.

Thank you very much for the links as well!

I still didn’t get what you actually want to replace … please … share the relevant part of the input data AND the expected output. So something like “before” and “after”

In the .bat it says;

SET /p VERSION=<c:\Folder\VersionTXT\Success.version.txt
SET /A VERSION=%VERSION%+1
>c:\Folder\VersionTXT\Success.version.txt ECHO %VERSION%
SET VERSION=10.0.33750.%VERSION%
ECHO %VERSION%

The SET VERSION, currently it says 10.0.33750. What I can make it say is 12.0.73820.0.33750, where I only want the 12.0.73820.

So your file content before is this:

SET /p VERSION=<c:\Folder\VersionTXT\Success.version.txt
SET /A VERSION=%VERSION%+1
>c:\Folder\VersionTXT\Success.version.txt ECHO %VERSION%
SET VERSION=10.0.33750.%VERSION%
ECHO %VERSION%

And you want to replace the version number with

12.0.73820

And the result should look like this:

SET /p VERSION=<c:\Folder\VersionTXT\Success.version.txt
SET /A VERSION=%VERSION%+1
>c:\Folder\VersionTXT\Success.version.txt ECHO %VERSION%
SET VERSION=12.0.73820.%VERSION%
ECHO %VERSION%

Right?

$Path = '.\MortenTest.bat'
$nyKundeVersion = '12.0.73820'
$Pattern =  '(?:(\d+))(?:\.(\d+))?(?:\.(\*|\d+))'
$Content = Get-Content $Path -Raw
$NewContent = $Content -replace $Pattern, $nyKundeVersion
$NewContent | Out-File $Path

Try it with test data!

Good morning Olaf,

Regex will look confusing for a while, won’t it? :slight_smile:

I’ve tried this out, but the .bat ends up looking like this now;

SET /p VERSION=<c:\VersionTXT\Success.version.txt
SET /A VERSION=%VERSION%+1
>c:\VersionTXT\Success.version.txt ECHO %VERSION%
SET VERSION=11.0.8472.11.0.8472.8472345.%VERSION%
ECHO %VERSION%

Before I launched the script, it said 10.0.12345.%VERSION% as the version number.

After running the script a couple of times , it seems the version number put into the .bat gets longer and longer;

SET /p VERSION=<c:VersionTXT\Success.version.txt
SET /A VERSION=%VERSION%+1
>c:VersionTXT\Success.version.txt ECHO %VERSION%
SET VERSION=11.0.84721.11.0.84721.11.0.84721.11.0.84721.11.0.84721.11.0.84721.11.0.84721.11.0.84721.%VERSION%
ECHO %VERSION%```

I cannot reproduce this behaviour. With the given content in the file no matter how often I run the code the version number changes after the first run and stays the same then.