Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing any content inbetween second and third underscore

I have a PowerShell Scriptline that replaces(deletes) characters between the second and third underscore with an "_":

get-childitem *.pdf | rename-item -newname { $_.name -replace '_\p{L}+, \p{L}+_', "_"}

Examples:

12345_00001_LastName, FirstName_09_2018_Text_MoreText.pdf
12345_00002_LastName, FirstName-SecondName_09_2018_Text_MoreText.pdf
12345_00003_LastName, FirstName SecondName_09_2018_Text_MoreText.pdf

This _\p{L}+, \p{L}+_ regex only works for the first example. To replace everything inbetween I have used _(?:[^_]*)_([^_]*)_ (according to regex101 this should almost work) but the output is:

12345_09_MoreText.pdf

The desired output would be:

 12345_00001_09_2018_Text_MoreText.pdf
 12345_00002_09_2018_Text_MoreText.pdf
 12345_00003_09_2018_Text_MoreText.pdf

How do I correctly replace the second and third underscore and everything inbetween with an "_"?

like image 886
Vid Man Avatar asked Jun 17 '26 16:06

Vid Man


1 Answers

If you don't want to use regex -

$files = get-childitem *.pdf        #get all pdf files
$ModifiedFiles, $New = @()  #declaring two arrays
foreach($file in $files)
{
    $ModifiedFiles = $file.split("_")
    $ModifiedFiles = $ModifiedFiles | Where-Object { $_ -ne $ModifiedFiles[2] }     #ommitting anything between second and third underscore
    $New = "$ModifiedFiles" -replace (" ", "_")
    Rename-Item -Path $file.FullName -NewName $New
}

Sample Data -

$files = "12345_00001_LastName, FirstName_09_2018_Text_MoreText.pdf", "12345_00002_LastName, FirstName-SecondName_09_2018_Text_MoreText.pdf", "12345_00003_LastName, FirstName SecondName_09_2018_Text_MoreText.pdf"
$ModifiedFiles, $New = @()  #declaring two arrays
foreach($file in $files)
{
    $ModifiedFiles = $file.split("_")
    $ModifiedFiles = $ModifiedFiles | Where-Object { $_ -ne $ModifiedFiles[2] }     #ommitting anything between second and third underscore
    $New = "$ModifiedFiles" -replace (" ", "_")
}
like image 103
Vivek Kumar Singh Avatar answered Jun 20 '26 07:06

Vivek Kumar Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!