如何删除先前字符串的一部分
How to remove part of previous string
我目前正在处理 SQL 服务器 JOB 文件,其中包含以下代码
@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
:
$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来得到这一行的行号。
我该怎么做?
I want to get the line number of this line by retrieving the line number of the line that I will remove -1.
How can I do this?
要找到该行号,您可以使用 LineNumber 并减去 1:
$number = (Select-String $file -pattern $stringtorremove | Select-Object -ExpandProperty LineNumber) - 1
嗨,我想我 post 我的解决方案,如果其他人有类似的问题:
$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 }
}
我目前正在处理 SQL 服务器 JOB 文件,其中包含以下代码
@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
:
$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来得到这一行的行号。
我该怎么做?
I want to get the line number of this line by retrieving the line number of the line that I will remove -1. How can I do this?
要找到该行号,您可以使用 LineNumber 并减去 1:
$number = (Select-String $file -pattern $stringtorremove | Select-Object -ExpandProperty LineNumber) - 1
嗨,我想我 post 我的解决方案,如果其他人有类似的问题:
$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 }
}