Replace text between string

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

$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"
}

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

Your code is a weird mix of PowerShell and dotNet and is badly formatted. :wink: 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.)

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

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

Have you tried:

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

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

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:

1 Like

Sorry, my reply was based on your comment.

replace by regex would not be possible?

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:

$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+)',','
}

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 :wink: I hope you are able to change your input format, otherwise you can talk with the Unix and Linux guys :wink: