0

I need help with this script. I have slowly gotten this far, but need help at this point.

I need a script that will move text from the end of a section to to a certain point in the file, and then delete the moved text. The text that is to be moved has markers and so does the location. I need to be able to delete the text after the move. Also need to be done on multiple txt files in the same directory.

For example:

Sample Input .txt

A;1;1;####; (#### is the location (1) marker)
B
B
B
====-1234 (==== is the find (1) marker)
A;1;1;####; (#### is the location (2) marker)
B
B
B
====-5678 (==== is the find (2) marker)

After processing

A;1;1;1234;
B
B
B
A;1;1;5678;
B
B
B

The text file can have multiple groupings like this. Need to do this for each grouping from top to bottom. Here is what I have so far, it just moves the text and doesn't delete.

$file = "C:\Users\NX07934\Documents\Projects\23045\Docs\SampleData\*.txt"
$old = "\####" 

$find = Get-ChildItem $file -recurse| Select-String -pattern "====-*"

$split = $find.ToString().Split("-")
$new = $split[1]


get-childitem "C:\Dir" -recurse -include *.txt | 
select -expand fullname |
    foreach 
    { 
        (Get-Content $_) -replace $old,$new |
        Set-Content $_            
    }

Thanks for any and all help!

1 Answer 1

1

Any help?

$text = 
@'
A;1;1;####;
B
B
B
====-1234
A;1;1;####;
B
B
B
====-5678
'@

$regex = 
@'
(?ms)(.+?####;
.+?)
====-(\d+)
'@

([regex]::matches($text,$regex) |
foreach {
$_.groups[1].value -replace '####',($_.groups[2].value)
}) -join ''

A;1;1;1234;
B
B
B
A;1;1;5678;
B
B
B

Edit: - to apply it to a collection of files:

$regex = 
@'
(?ms)(.+?####;
.+?)
====-(\d+)
'@

Get-Childitem -Path C:\somedir -Filter *.txt |
foreach {

    $Text = Get-Content $_ -Raw

    ([regex]::matches($text,$regex) |
    foreach {
    $_.groups[1].value -replace '####',($_.groups[2].value)
    }) -join '' |
    Set-Content $_.FullName
 }
Sign up to request clarification or add additional context in comments.

3 Comments

that worked for that instance, but i need it to do that in multiple files. open the .txt search for ====-1234 and replace the #### with the number. there can be multiple groups in one file and multiple files in the directory.
Left an example. It handles multiple instances per file. You just have to set up a loop for your file collection.
i tried the snippet, and i am getting an error at the -Raw line: Get-Content : A parameter cannot be found that matches parameter name 'raw'. At C:\Users\NX07934\Documents\Projects\23045\Docs\SampleData\PowerShellTest_Regex.ps1:11 char:32 + $text = Get-Content $_ -raw <<<< + CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetContentCommand

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.