Select-String / sls

Select-String / sls

Dieser Powershell-Befehl entfernt in einer Batchdatei alle Zeilen, %tempFile%die die Textzeichenfolge enthaltenfoo

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

Der -notlikeBefehl funktioniert auch ohne die Verwendung von Asterisken:

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

Ich habe Zeilen, %tempFile%die einen Backslash enthalten \, und möchte diese Zeilen entfernen. Die übliche Lösung besteht darin, den Backslash zu verdoppeln, um ihn auf diese Weise zu maskieren. \\Dies funktioniert zwar bei Verwendung von Ersetzungsbefehlen, funktioniert bei mir jedoch nicht, wenn ich den obigen Befehl verwende -notlike.

Keines dieser Beispiele funktioniert:

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 }"

Ich wundere mich warum?

Vielen Dank im Voraus an alle, die Licht in diese Sache bringen können.

Antwort1

@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%"

  • Ergebnis in:
 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

catWird zum Speichern des Dateiinhalts durch Subtrahieren von Zeilen mit dem Zeichen verwendet \, das mit adicional maskiert werden kann \ ===> \\.

Und versuchen Sie, zu ersetzen ... -notlike '\\'durch ... -NotMatch '\\'oder... -Not '\\'


Select-String / sls

  • Kurze Beschreibung:

Die Vergleichsoperatoren in PowerShell können entweder zwei Werte vergleichen
oder Elemente einer Sammlung anhand eines Eingabewerts filtern.

  • Lange Beschreibung:

Mit Vergleichsoperatoren können Sie Werte vergleichen oder Werte finden, die
bestimmten Mustern entsprechen. PowerShell enthält die folgenden
Vergleichsoperatoren:

  • Gleichwertigkeit
-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
  • Dazu passend
-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

verwandte Informationen