2017-12-14 12 views
0

ファイル内の文字列を見つけて置き換え、PowerShellの元のファイルに保存しようとしています。PowerShell-replace関数

私は

(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は '(ゲット・コンテンツ「C解釈します破るエスケープせずに新しい行に' -replace'オペレータを置く場合:\ Users \ユーザーアノン\デスクトップ\を"replace"と同じ行に '-replace'オペレーションを置いてください。' -replace'オペレーションを 'Get- –

+0

@AnsgarWiechers - 答えとして提案すると、クエレンの問題を解決します –

+0

@JeffZeitlin私はこれを "シンプルなタイポグラフィエラー"のケースと考え、クローに投票しましたse。 –

答えて

1

コードに行の継続を表すものはなく、インタープリタは同じコマンドの一部として-replace演算子を認識していません。これを解決するには、改行をエスケープするか、コマンドを同じ行に置くという2つの方法があります。

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

OR

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