選擇字串/sls

選擇字串/sls

此 Powershell 命令在批次檔中運行,以刪除%tempFile%包含文字字串的所有行foo

powershell -NoProfile -ExecutionPolicy Bypass -Command "gci %tempFile% | foreach { (cat $_.FullName | ? { $_ -notlike '*foo*' }) | set-content $_.FullName -Force }"

-notlike命令在不使用星號的情況下也可以工作:

| ? { $_ -notlike 'foo' }) |

我有一些行%tempFile%包含反斜線字符\,並且希望刪除這些行。通常的解決方案是將反斜線字元加倍以像這樣轉義它\\,雖然在使用替換命令時確實有效,但當我使用上述命令時它對我不起作用-notlike

這些例子都不起作用:

powershell -NoProfile -ExecutionPolicy Bypass -Command "gci %tempFile% | foreach { (cat $_.FullName | ? { $_ -notlike '*\\*' }) | set-content $_.FullName -Force }"
powershell -NoProfile -ExecutionPolicy Bypass -Command "gci %tempFile% | foreach { (cat $_.FullName | ? { $_ -notlike '*\*' }) | set-content $_.FullName -Force }"
powershell -NoProfile -ExecutionPolicy Bypass -Command "gci %tempFile% | foreach { (cat $_.FullName | ? { $_ -notlike '\\' }) | set-content $_.FullName -Force }"
powershell -NoProfile -ExecutionPolicy Bypass -Command "gci %tempFile% | foreach { (cat $_.FullName | ? { $_ -notlike '\' }) | set-content $_.FullName -Force }"

我想知道為什麼?

預先感謝任何能夠闡明這一點的人。

答案1

@echo off

>"%temp%\Q1767780.txt" ^
    (
     echo+ Lorem ipsum dolor sit amet. Qui magnam adipisci id officiis 
     echo+ temporibus non minima molestias ex animi tempora et eveniet atque.
     echo\ Et ratione unde qui numquam libero est voluptas nesciunt.\
     echo+ 
     echo\ Id \corporis dolorum vel debitis dolore ut voluptas temporibus est 
     echo+ obcaecati harum eos earum fugit et reprehenderit temporibus eos 
     echo\ voluptas vero.\
     echo\               \
    ) && set "_tempFile=%temp%\Q1767780.txt"

PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "$Strs=(cat "%_tempFile%" | sls -Not '\\'); sc "%_tempFile%" $Strs -Force"
     
type "%_tempFile%"

  • 導致:
 Lorem ipsum dolor sit amet. Qui magnam adipisci id officis
 temporibus non minima molestias ex animi tempora et eveniet atque.

 obcaecati harum eos earum fugit et reprehenderit temporibus eos

用於cat透過減去帶有字元的行來儲存檔案的內容\,可以使用 adicional 進行轉義\ ===> \\

並嘗試替換... -notlike '\\'... -NotMatch '\\'... -Not '\\'


選擇字串/sls

  • 簡短的介紹:

PowerShell 中的比較運算子可以比較兩個值
,也可以根據輸入值過濾集合的元素。

  • 詳細描述:

比較運算子可讓您比較值或尋找
與指定模式相符的值。 PowerShell 包含以下
比較運算子:

  • 平等
-eq, -ieq, -ceq - equals
-ne, -ine, -cne - not equals
-gt, -igt, -cgt - greater than
-ge, -ige, -cge - greater than or equal
-lt, -ilt, -clt - less than
-le, -ile, -cle - less than or equal
  • 匹配
-like, -ilike, -clike - string matches wildcard pattern
-notlike, -inotlike, -cnotlike - string doesn't match wildcard pattern
-match, -imatch, -cmatch - string matches regex pattern
-notmatch, -inotmatch, -cnotmatch - string doesn't match regex pattern

相關內容