我對 Powershell 相當陌生,并且遇到了一些麻煩。
我試圖在腳本中找到字串“o_dwh”,但如果在這個陳述句之前有 exec - “exec o_dwh” - 我不想選擇它。我怎么做?
到目前為止,我有這個:
Get-ChildItem -Path "$packagepath\Scripts\" -Include *.txt -Recurse | Select-String -Pattern "o_dwh"
我試過這個,但我知道這是錯誤的:
Get-ChildItem -Path "$packagepath\Scripts\" -Include *.txt -Recurse | Select-String -Pattern "o_dwh" and "exec o_dwh" -notmatch
uj5u.com熱心網友回復:
我認為這應該可以很好地使用否定的lookbehind(?<!exec ) 來匹配那些o_dwh前面的任何東西,但是exec ,一個簡單的例子:
# matches only last 2 lines
@'
something something exec o_dwh
exec o_dwh something
something o_dwh something
o_dwh something something
'@ -split '\r?\n' | Select-String '(?<!exec )o_dwh'
因此,假設這是您所需要的,那么代碼將是(-Filter這里更可取而不是-Include):
Get-ChildItem -Path "$packagepath\Scripts" -Filter *.txt -Recurse |
Select-String -Pattern '(?<!exec )o_dwh'
有關詳細資訊,請參閱https://regex101.com/r/VupVNA/1。
uj5u.com熱心網友回復:
這也解決了我的問題,但它不是那么優雅。
Get-ChildItem -Path "$packagepath\Scripts\" -Include *.txt -Recurse | Select-String -Pattern "o_dwh" | Where-Object line -NotMatch "exec o_dwh"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/523411.html
上一篇:如何連接字串
下一篇:給單個輸出
