
이 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
있는 줄을 빼서 파일의 내용을 저장하는 데 사용합니다 .\
\ ===> \\
그리고 교체 ... -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