[英]Windows batch or powershell. Replace “{\n}” text
I've got a text file that contains: 我有一个包含以下内容的文本文件:
title
{
}
I need to replace {\\n} with "mytext". 我需要用“mytext”替换{\\ n}。
I tried to use this: 我试着用这个:
powershell -Command "(gc file.txt) -replace \"{\n}\", "mytext"
with no success. 没有成功。
Could anyone help me? 谁能帮助我? Thank you. 谢谢。
In order for -replace
to operate across lines , you must read the entire file into memory as a single string , which is what Get-Content -Raw
( gc -raw
) does (PSv3+). 为了-replace
跨线运行,必须将整个文件读入内存作为单个字符串 ,这是Get-Content -Raw
( gc -raw
)确实(PSv3 +)。
Additionally, to account for the typical CRLF ( \\r\\n
) line endings on Windows, use \\r?\\n
to match both them and LF-only ( \\n
) line endings, as Mathias R. Jessen suggests. 此外,要考虑Windows上典型的CRLF( \\r\\n
)行结尾,请使用\\r?\\n
来匹配它们和仅LF( \\n
)行结尾,正如Mathias R. Jessen所建议的那样。
Along with corrections to and simplification of quoting, we get: 随着对引用的更正和简化,我们得到:
powershell -Command "(gc -raw file.txt) -replace '\{\r?\n\}', 'mytext'"
Please view mklement0's answer instead for this question, as mine will not work. 请查看mklement0的答案,而不是这个问题,因为我的不行。 I will leave it here as a method of literal string replacement. 我将它留在这里作为文字字符串替换的方法。
Give this a shot in a PowerShell console. 在PowerShell控制台中进行一次拍摄。 If it works you can always save it as a script in a .ps1 file. 如果它工作,您可以始终将其保存为.ps1文件中的脚本。
$FilePath = "\\Server\Share\PathToFile\File.txt"
$ToBeReplaced = "{\n}"
$ReplaceWith = "mytext"
$File = Get-Content $FilePath
$File | foreach { $_.Replace($ToBeReplaced,$ReplaceWith) } | Set-Content $FilePath
Text file that contains: 文本文件包含:
title
{
}
Goal: replace {\\n} with "mytext". 目标:将{\\ n}替换为“mytext”。
OP attempted: OP试图:
powershell -Command "(gc file.txt) -replace \"{\n}\", "mytext"
First, Get-Content doesn't returned the NewLines (by default) but rather an array of strings split on the newlines so this is the most direct translation of the command you attempted: 首先,Get-Content不返回NewLines(默认情况下),而是返回在换行符上拆分的字符串数组,因此这是您尝试的命令的最直接转换:
(Get-Content file.txt) -join 'mytext'
Or use -Raw to prevent Get-Content from spliting and to just return one string: 或者使用-Raw来防止Get-Content分裂并返回一个字符串:
(get-content -raw .\PSRLKeyBindings.txt) -replace '\n', 'mytext'
This works without using the Regex multiline modifiers etc. (I was uncertain that would be the case until I tested.) 这可以在不使用正则表达式多线修改器等的情况下工作。(我不确定在我测试之前会出现这种情况。)
If you really wish to run it from some arbitrary place (cmd.exe, Run dialog, called from a shortcut) then you need the "powershell -command" but if you just run it from the PowerShell console then you can omit that as above. 如果你真的希望从任意位置运行它(cmd.exe,运行对话框,从快捷方式调用),那么你需要“powershell -command”,但如果你只是从PowerShell控制台运行它,那么你可以省略它,如上所述。
Here's the "starting PowerShell version -- though I use -noprofile because my extensive profile takes about 10 seconds to initialize: 这是“启动PowerShell版本 - 虽然我使用-noprofile,因为我的大量配置文件需要大约10秒来初始化:
powershell -noprofile -Command "(get-content -first 10 .\PSRLKeyBindings.txt) -join 'mytext'"
The quotes are necessary to make it all on "command parameter" -- or you can wrap it in curly braces -command { command here } 引号必须在“命令参数”上完成 - 或者你可以用花括号括起来 - 命令{command here}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.