1

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

2 Answers 2

2

You could do a regular expression replacement with a negative lookbehind and a positive lookahead:

$line -replace '(?<!\\)(?=.{6}$)', '\'

(?<!...) is a negative lookbehind, basically meaning "this does not exist before the match", "this" being a literal backslash here.
(?=...) is a positive lookahead, meaning "this does exist after the match", "this" being a sequence of 6 characters (.{6}) at the end of the string ($) here.

Both lookahead and lookbehind expressions don't become part of the match, so the above operation simply replaces the empty string between the negative lookbehind and the positive lookahead with a backslash (thus effectively inserting that character into the string), but only if the 6 characters at the end of the string are not already preceded by a backslash.

Sign up to request clarification or add additional context in comments.

2 Comments

That is exactly what I was looking for! Thx Now I only need to find out how to replace that character directly into the file.
(Get-Content filename) -replace ... | Set-Content filename
1

This can be done with regex replace too (one-liner):

Assume your special character is a % and you always want to put it at the third last position:

"12345689" -replace '^(.*?)%?(.{2})$', '$1%$2'

Result:

1234567%89

If you already have the % in place, it won't change:

"123456%89" -replace '^(.*?)%?(.{2})$', '$1%$2'

Result:

1234567%89

Regex Demo.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.