[英]Powershell how to add new line after certain string
我是腳本新手。 我正在嘗試解決一個問題,當時我正在查看Windows批處理和Powershell。 我發現了這個,但我不知道為什么它不起作用:
我有具有以下內容的input.txt文件:
<a> TEXT </a>
<c> text asdrtlj </c>
<a> another text </a>
我想在每個<a>
之前添加新行,所以我嘗試了以下操作:
powershell -Command "(gc input.txt) -replace '<a>', '`r`n<a>' | Out-File output.txt"
那添加換行符不起作用。 你能幫我嗎?
PS:在搜索解決方案時,我發現了很多復雜的代碼,但我還不了解它們,因此,如果您向我推薦一些不錯的教程,從這里開始使用這種語言,我將非常感謝。
這里有幾個問題。 由於<>字符,通過powershell的-command
開關執行命令會導致一些保留字符問題。
而是打開PowerShell.exe窗口(開始->運行-> powershell)並直接執行命令。
您還需要使用雙引號而不是單引號,以使諸如r和n的特殊代碼起作用。 同樣,`n應該足夠:
(gc input.txt) -replace '<a>', "`n<a>" | Out-File output.txt
我們需要更多的引號。
powershell.exe -command "(gc 1_105.jpg) -replace '<a>', """"`r`n<a>""""|Out-File output.txt"
或小型重構怎么樣?
powershell.exe -Command "gc input.txt|%{$_.replace('<a>',[environment]::newline+'<a>')}|Out-File output.txt"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.