# Replace in text file strings beteween specific text marker using wildcards

**URL:** <https://forums.powershell.org/t/replace-in-text-file-strings-beteween-specific-text-marker-using-wildcards/24942>\
**Category:** PowerShell Help\
**Created:** [October 30, 2024, 11:37am UTC](https://forums.powershell.org/t/replace-in-text-file-strings-beteween-specific-text-marker-using-wildcards/24942 "2024-10-30T11:37:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bovirus](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@bovirus](https://forums.powershell.org/u/bovirus)\
**Post date:** [October 30, 2024, 11:37am UTC](https://forums.powershell.org/t/replace-in-text-file-strings-beteween-specific-text-marker-using-wildcards/24942/1 "2024-10-30T11:37:27Z")

</div>

Hi.

I like to read a text and replace (remove) text between specif makler but using wildcards.

Ex Example1

{aaa}value1  
{bbbbbbb}value2

I would remove {x} where x can be any character strings type and lenght to get just a list like

value1  
value2

and save in an output etxt file.

- Example 2

Input etxt file like this

value1  
value1

I’d like to remove and and et a list like

value1  
value2

and save it in an output etxt file.

How can do it with Powershell?

Thanks.

---

<div class="post-metadata">

**Author:** ![grey0ut](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/grey0ut/32/5310_2.png) [@grey0ut](https://forums.powershell.org/u/grey0ut)\
**Post date:** [October 30, 2024, 2:50pm UTC](https://forums.powershell.org/t/replace-in-text-file-strings-beteween-specific-text-marker-using-wildcards/24942/2 "2024-10-30T14:50:02Z")

</div>

Have you tried anything yet in Powershell? Most forums aren’t about writing code _for_ people but rather helping people with what they’ve got.  
Your first example should be achievable with [“Replace”](https://ss64.com/ps/replace.html) and regex. I suggest using [Regex101.com](http://Regex101.com) to test your matching statements.

Then your second one, if I’m understanding it right, could probably be done with Sort-Object and the -Unique switch parameter.

---

<div class="post-metadata">

**Author:** ![ambre](https://avatars.discourse-cdn.com/v4/letter/a/b2d939/32.png) [@ambre](https://forums.powershell.org/u/ambre)\
**Post date:** [November 8, 2024, 1:03pm UTC](https://forums.powershell.org/t/replace-in-text-file-strings-beteween-specific-text-marker-using-wildcards/24942/3 "2024-11-08T13:03:38Z")

</div>

If you want to remove all characters before a specified value:

```auto
$string = "xyzValue"
$newString = $string -replace '^.*Value', 'Value'
Write-Output $newString

```

if you want to remove all characters before and after a specified value:

```auto
$string = "XyZValuea1b2c3"
$newString = $string -replace '^.*Value', 'Value' -replace 'Value.*$', 'Value'
Write-Output $newString

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [December 8, 2024, 1:03pm UTC](https://forums.powershell.org/t/replace-in-text-file-strings-beteween-specific-text-marker-using-wildcards/24942/4 "2024-12-08T13:03:41Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
