# Replace text between string

**URL:** <https://forums.powershell.org/t/replace-text-between-string/19523>\
**Category:** PowerShell Help\
**Created:** [June 5, 2022, 6:05pm UTC](https://forums.powershell.org/t/replace-text-between-string/19523 "2022-06-05T18:05:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![taps](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@taps](https://forums.powershell.org/u/taps)\
**Post date:** [June 5, 2022, 6:05pm UTC](https://forums.powershell.org/t/replace-text-between-string/19523/1 "2022-06-05T18:05:51Z")

</div>

Hello, i need this script. How get output with replaced value? Thank you.

```auto
$temp = $env:TEMP 
$path = $temp+"\file.txt"
$output = ".\"
if([System.IO.File]::Exists($path)){
    $text = Get-Content -Path $path
    $import = [regex]::Matches($text, "(?<=text2: )(.+?)(?=\s+VS)").value   
    $subject = [regex]::Matches($text, "(?<=text1: )(.+?)(?=\s+test)").value  
    $date = [regex]::Matches($text, "\d{1,2}\.\d{1,2}\.\d{4}").value[0]
    $dates = [regex]::Matches($text, "\d{1,2}\.\d{1,2}\.\d{4}").value
    $name = $date+"_"+$subject+"_"+$import
    $csv = $text | % {$_ -replace ("`t", ";")}
    ForEach ($dates_item in $dates){
      $numbers = [Regex]::Matches($csv, "(?<="+$dates_item+";)(.+?);").value
      foreach($number in $numbers){
      $csv = $number.Replace(".", ",")
      }
    }  
    Write-Host "Complete"
}
else{
Write-Host "Not found"
}

```

---

<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:** [June 5, 2022, 7:16pm UTC](https://forums.powershell.org/t/replace-text-between-string/19523/2 "2022-06-05T19:16:17Z")

</div>

It will be hard and probably error prone to recommend something meaningful when we do not know what you’re actually trying to do. 😉

Your code is a weird mix of PowerShell and dotNet and is badly formatted. 😉 Could you please explain with a few words what your goal is, what your situation before is and what the expected result is?

(When you post smaple data, please format it as code as well. Thanks in advance.)

---

<div class="post-metadata">

**Author:** ![taps](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@taps](https://forums.powershell.org/u/taps)\
**Post date:** [June 7, 2022, 6:40pm UTC](https://forums.powershell.org/t/replace-text-between-string/19523/3 "2022-06-07T18:40:19Z")

</div>

I have this txt file. I need replace tab to semicolon and replace dot to comma in price

```auto
my export file
condition transaction: 111111111
					     Items: all

text2: 4604 VS	old XS expire	price	sender
		2536985678 25.5.2022	10.96	Stephan
		1111000001 25.5.2022	600	John
		2222275016 25.5.2022	8.04	Andy

		count: 3	suma 619

```

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [June 7, 2022, 8:18pm UTC](https://forums.powershell.org/t/replace-text-between-string/19523/5 "2022-06-07T20:18:15Z")

</div>

Have you tried:

```auto
$text.Replace("`t", ';').Replace('.', ',')

```

---

<div class="post-metadata">

**Author:** ![taps](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@taps](https://forums.powershell.org/u/taps)\
**Post date:** [June 8, 2022, 7:15am UTC](https://forums.powershell.org/t/replace-text-between-string/19523/6 "2022-06-08T07:15:50Z")

</div>

I replaced in date, I dont wont it. I need replace dot only in price

---

<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:** [June 8, 2022, 8:33am UTC](https://forums.powershell.org/t/replace-text-between-string/19523/7 "2022-06-08T08:33:07Z")

</div>

Depending on your actual structure in your input file you have several options to extract the data from it. You may watch this video to get an idea what’s possible:

[![](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/a/ae17ad90a341726ccae4dbaf824f7e5c39aec98b.jpeg "6 Weltner Sophisitcated Techniques of Plain Text Parsing") ](https://www.youtube.com/watch?v=Hkzd8spCfCU)

---

<div class="post-metadata">

**Author:** ![tonyd](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tonyd/32/1041_2.png) [@tonyd](https://forums.powershell.org/u/tonyd)\
**Post date:** [June 8, 2022, 5:21pm UTC](https://forums.powershell.org/t/replace-text-between-string/19523/8 "2022-06-08T17:21:40Z")

</div>

> [@taps](#):
>
> I have this txt file. I need replace tab to semicolon and replace dot to comma in price

Sorry, my reply was based on your comment.

---

<div class="post-metadata">

**Author:** ![taps](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@taps](https://forums.powershell.org/u/taps)\
**Post date:** [June 9, 2022, 5:46am UTC](https://forums.powershell.org/t/replace-text-between-string/19523/9 "2022-06-09T05:46:44Z")

</div>

replace by regex would not be possible?

---

<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:** [June 9, 2022, 8:00am UTC](https://forums.powershell.org/t/replace-text-between-string/19523/10 "2022-06-09T08:00:31Z")

</div>

Yes it would. But you did not provide enough information to recommend something meaningful. For the particular sample input text you posted it could be done like this:

```auto
$InputText = @"
my export file
condition transaction: 111111111
					     Items: all

text2: 4604 VS	old XS expire	price	sender
		2536985678 25.5.2022	10.96	Stephan
		1111000001 25.5.2022	600	John
		2222275016 25.5.2022	8.04	Andy

		count: 3	suma 619
"@

foreach ($line in $InputText) {
    $line -replace '(?<=\d{1,2}\.\d{1,2}\.\d{4}\s+\d+)\.(?=\d+)',','
}

```

---

<div class="post-metadata">

**Author:** ![MrAutomation](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@MrAutomation](https://forums.powershell.org/u/MrAutomation)\
**Post date:** [June 12, 2022, 3:27pm UTC](https://forums.powershell.org/t/replace-text-between-string/19523/11 "2022-06-12T15:27:53Z")

</div>

I would suggest to use a more object based input file, like xml or json. Plaintext is unstructured by default and you need all kinds of regular expressions to solve problems that would not exist if one used structured input. If you are not able to use a normal/modern input (probably Unix or Linux output) you will be stuck with terrible regexes that potentially break with every little change in the input file. Parsing text files is a painful process, just ask any Unix or Linux guy, they are aware of the horror involved 😉 I hope you are able to change your input format, otherwise you can talk with the Unix and Linux guys 😉

---

<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:17pm UTC](https://forums.powershell.org/t/replace-text-between-string/19523/12 "2024-05-16T20:17:49Z")

</div>


