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

**URL:** <https://forums.powershell.org/t/how-to-remove-from-character-x-to-character-y-in-a-text-file/21209>\
**Category:** PowerShell Help\
**Created:** [December 22, 2022, 12:19pm UTC](https://forums.powershell.org/t/how-to-remove-from-character-x-to-character-y-in-a-text-file/21209 "2022-12-22T12:19:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![MortenBak](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@MortenBak](https://forums.powershell.org/u/MortenBak)\
**Post date:** [December 22, 2022, 12:19pm UTC](https://forums.powershell.org/t/how-to-remove-from-character-x-to-character-y-in-a-text-file/21209/1 "2022-12-22T12:19:47Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 22, 2022, 12:25pm UTC](https://forums.powershell.org/t/how-to-remove-from-character-x-to-character-y-in-a-text-file/21209/2 "2022-12-22T12:25:40Z")

</div>

Morten,  
Welcome to the forum. 👋🏽

> [@MortenBak](#):
>
> Any help on how to get this written is much appreciated!

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

> [@MortenBak](#):
>
> 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.

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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

---

<div class="post-metadata">

**Author:** ![MortenBak](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@MortenBak](https://forums.powershell.org/u/MortenBak)\
**Post date:** [December 22, 2022, 12:48pm UTC](https://forums.powershell.org/t/how-to-remove-from-character-x-to-character-y-in-a-text-file/21209/3 "2022-12-22T12:48:04Z")

</div>

Hello Olaf!

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

```auto
(
    (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. 🙂

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 22, 2022, 1:00pm UTC](https://forums.powershell.org/t/how-to-remove-from-character-x-to-character-y-in-a-text-file/21209/4 "2022-12-22T13:00:30Z")

</div>

So it is about version numbers?! 😏

> [@MortenBak](#):
>
> Sorry that I cant share any of the actual files im working with, it has sensitive information. 🙂

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. 😉

> [@MortenBak](#):
>
> ` ) -replace '6|7|8|9|10|11|12|13|14', $nyKundeVersion`

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. 😉

> [@MortenBak](#):
>
> ```auto
> ) -replace ($arrayBatch.Substring($substring,6),"")
> 
> ```

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

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:

> **[Regex for Numbers and Number Range (With Examples) - Regex Tutorial - Regular...](https://www.regextutorial.org/regex-for-numbers-and-ranges.php)**
>
> In this article you will learn how to match numbers and number range in Regular expressions. The Regex number range include matching 0 to 9, 1 to 9, 0 to 10, 1 to 10, 1 to 12, 1 to 16 and 1-31, 1-32, 0-99, 0-100, 1-100,1-127, 0-255, 0-999, 1-999,...

> **[regex101: Version numbers](https://regex101.com/library/8eyDJb?orderBy=RELEVANCE&search=version)**
>
> Search, filter and view user submitted regular expressions in the regex library. Over 20,000 entries, and counting!

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

> **[Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp...](https://www.regular-expressions.info/)**
>
> At Regular-Expressions.info you will find a wide range of in-depth information about a powerful search pattern language called regular expressions.

---

<div class="post-metadata">

**Author:** ![MortenBak](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@MortenBak](https://forums.powershell.org/u/MortenBak)\
**Post date:** [December 22, 2022, 1:23pm UTC](https://forums.powershell.org/t/how-to-remove-from-character-x-to-character-y-in-a-text-file/21209/5 "2022-12-22T13:23:37Z")

</div>

“It’s always the damn version numbers!” 😃

> [@Olaf](#):
>
> So you should obfuscate sensitive information and replace them with random text. The information you shared so far are still pretty insufficient.

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;

```auto
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.

> [@Olaf](#):
>
> 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.

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.

> [@Olaf](#):
>
> Since the replace operator works with regex I wouldn’t expect that to be a valid regex pattern.

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!

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 22, 2022, 1:37pm UTC](https://forums.powershell.org/t/how-to-remove-from-character-x-to-character-y-in-a-text-file/21209/6 "2022-12-22T13:37:53Z")

</div>

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”**

---

<div class="post-metadata">

**Author:** ![MortenBak](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@MortenBak](https://forums.powershell.org/u/MortenBak)\
**Post date:** [December 22, 2022, 1:53pm UTC](https://forums.powershell.org/t/how-to-remove-from-character-x-to-character-y-in-a-text-file/21209/7 "2022-12-22T13:53:47Z")

</div>

In the .bat it says;

```auto
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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 22, 2022, 2:11pm UTC](https://forums.powershell.org/t/how-to-remove-from-character-x-to-character-y-in-a-text-file/21209/8 "2022-12-22T14:11:54Z")

</div>

So your file content before is this:

```auto
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

```auto
12.0.73820

```

And the result should look like this:

```auto
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?

```auto
$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!

---

<div class="post-metadata">

**Author:** ![MortenBak](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@MortenBak](https://forums.powershell.org/u/MortenBak)\
**Post date:** [December 23, 2022, 6:42am UTC](https://forums.powershell.org/t/how-to-remove-from-character-x-to-character-y-in-a-text-file/21209/9 "2022-12-23T06:42:54Z")

</div>

Good morning Olaf,

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

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

```auto
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;

````auto
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%```
````

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [December 23, 2022, 7:04am UTC](https://forums.powershell.org/t/how-to-remove-from-character-x-to-character-y-in-a-text-file/21209/10 "2022-12-23T07:04:25Z")

</div>

> [@MortenBak](#):
>
> After running the script a couple of times , it seems the version number put into the .bat gets longer and longer;

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/how-to-remove-from-character-x-to-character-y-in-a-text-file/21209/11 "2024-05-16T20:16:10Z")

</div>


