I have a text file, where I need to check if a special character is present at a special position.
First I find out, if the character is present or not. If it is not, I want to add it at the position where it is missing. The problem is, that the number of characters in front of the character I am checking, are changing. Which means I can not simply add the character at the same position all the time using insert.
The end of the string looks always the same though. Is there a way, where I can add the character directly at the position where it is missing or insert it at a position after counting a number of characters backwards from the end of the string?
At the moment my code looks like this:
$content = Get-Content Path\to\file.txt
foreach ($line in $content) {
if ($line[-6] -ne '\') {
#Add character here
}
echo $line
}
EDIT:
This does what I want, but I think it is far away from a good solution:
$content = Get-Content path\to\file.txt
foreach ($line in $content) {
if ($line[-25] -ne '\') {
$new_line = $line.Split(';')[0]
$new_line1 = $line.Split(';')[1] + '\' | % {$_ -replace '"', ''}
$new_line2 = $line.Split(';')[2]
$test = Write-Host $new_line';"'$new_line1'";'$new_line2
}
}
echo $test