2017-12-14 1 views
0

파일의 문자열을 찾아서 바꾸고 PowerShell의 원본 파일에 저장하려고합니다.PowerShell -replace function

나는

(Get-Content "C:\Users\anon\Desktop\test.txt") 
-replace 'apple', 'apple1' 
-replace 'bear' , 'bear1' | 
Out-File test1.txt 
pause 

그러나, 나는 "abcd".replace() 벌금과 문서 -replace에 따른 너무 작업을해야 사용할 수있었습니다

 
-replace : The term '-replace' is not recognized as the name of a cmdlet, function, 
script file, or operable program. Check the spelling of the name, or if a path was 
included, verify that the path is correct and try again. 
At C:\Users\Xing Chen\Desktop\test.ps1:2 char:1 
+ -replace 'apple', 'apple1' 
+ ~~~~~~~~ 
    + CategoryInfo   : ObjectNotFound: (-replace:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

점점 계속 일을 시도했습니다.

+5

당신은 라인이 PowerShell을'(은 Get-내용 "C 해석합니다 휴식 탈출하지 않고 새로운 라인에'-replace' 연산자를 넣으면 : \ 사용자 \ 곧 \ 바탕 화면 \를 '-replace' 연산을'Get-Text '와 같은 행에 넣고'-replace '연산을' –

+0

@AnsgarWiechers - 대답으로 제안하면 querent의 문제를 해결할 것입니다. –

+0

@JeffZeitlin 저는 이것을 "간단한 타이포그래피 오류"의 경우로 생각하고 clo에 투표했습니다. se. –

답변

1

코드에 줄 연속을 나타내는 것은 없으며 해석기는 동일한 명령의 일부로 -replace 연산자를 보지 못합니다. 이를 해결할 수있는 두 가지 옵션이 있습니다 : 개행 문자를 이스케이프 처리하거나 명령을 같은 행에 두는 것.

@(Get-Content "C:\Users\anon\Desktop\test.txt") -replace 'apple','apple1' -replace 'bear','bear1' | 
    Out-File -FilePath test1.txt 
Pause 

또는

@(Get-Content "C:\Users\anon\Desktop\test.txt") ` 
-replace 'apple','apple1' ` 
-replace 'bear','bear1' | 
    Out-File -FilePath test1.txt 
Pause