2016-10-07 91 views
1

我目前正在製作一個包含下面的代碼如何刪除以前的字符串

@active_end_date = 99991231, 
@active_start_time = 0, 
@active_end_time = 235959, 
@schedule_uid = N'59cbfb7d-67c7-495c-810d-0ca7a6357f9c' 

IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback 

我使用PowerShell來去除@schedule_uid這個代碼SQL Server作業文件的一部分:

$orgineel_localfolder = "C:\Users\Desktop\Jobs_from_Server\aangepast" 

$stringtorremove = '@schedule_uid=' 

$files = Get-ChildItem $orgineel_localfolder *.sql 

ForEach ($file in $files) 
{ 
    $linenumber = Select-String $file -pattern $stringtorremove  
    $removecomma = $linenumber -replace",","" 

    $fileName = "C:\Users\EX27740\Desktop\Jobs_from_Server\Orgineel\" + $file.Name.replace("_Orgineel","_nieuwe") # Vervangt alle Orgineel maar niet erg script blijft goed 

    (Get-Content $file.fullname) -notmatch $stringtorremove | Out-File $fileName 
    } 

我的問題是在上一行@active_end_time = 235959,。由於此行中的數據並不總是@active_end_time,我想通過檢索要刪除的行的行號-1來獲取此行的行號。

我該怎麼做?

回答

0

嗨以爲我後我的解決方案,這一點,如果任何人有過類似的問題:

$orgineel_localfolder = "C:\Users\Desktop\Jobs_from_Server\aangepast" 

$stringtorremove = '@schedule_uid=' 

ForEach ($file in $files){ 
#gets the line number of the value 
$huidigeregel = Select-String $file -pattern $stringtorremove | Select-Object -ExpandProperty 'LineNumber' 

if ([string]::IsNullOrWhitespace($huidigeregel)){ 
'Deze job heeft geen @schedule_uid= ' 
} else { 
#get the right line by subtracting the current line with -2 
$goederegel = $huidigeregel -2 
$valuewithcomma = get-content $file.fullname | select -Index $goederegel 
$valuewithoutcomma = $valuewithcomma -replace",","" 
$orginalithcomma= Get-Content $file.fullname 
} 

$fileName = "C:\Users\Desktop\Jobs_from_Server\aangepast\" + $file.Name.replace("_Orgineel","_nieuwe") 

if ([string]::IsNullOrWhitespace($huidigeregel)){ 
(Get-Content $file.fullname) -notmatch $stringtorremove | Out-File $fileName 
} else { 
(Get-Content $file.fullname) -replace $valuewithcomma, $valuewithoutcomma -notmatch $stringtorremove | Out-File $fileName } 
} 
0

我想通過檢索我將刪除的行的行號-1來獲取該行的行號。 我該怎麼做?

要找到可以使用LineNumber該行號和減去1:

$number = (Select-String $file -pattern $stringtorremove | Select-Object -ExpandProperty LineNumber) - 1