文字列の選択 / 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 の比較演算子は、2 つの値を比較したり
、コレクションの要素を入力値に対してフィルター処理したりできます。

  • 長い説明:

比較演算子を使用すると、値を比較したり、指定したパターンに一致する値を検索したりできます
。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

関連情報