简体   繁体   English

Windows批处理或PowerShell。 替换“{\\ n}”文本

[英]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 -Rawgc -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.

相关问题 无法在 Windows Powershell 上使用 cd。 找不到路径 - Cannot use cd on Windows Powershell. Path not found 使用 PowerShell 远程安装 Windows 更新。 (一些 PowerCLI) - Installing windows updates remotely, using PowerShell. (some PowerCLI) 使用Powershell在Windows窗体中模拟按钮单击。 - Simulate button clicks in a windows form using powershell. 使用Windows Batch替换文本文件中的一行 - Replace a line in a text file using windows batch Windows Batch - 查找一行并替换为文本 - Windows Batch - Find a line and replace with text Powershell:如何替换批处理文件中带引号的文本 - Powershell: how to replace quoted text from a batch file Windows 容器访问被拒绝。 (0x5)。 使用 Powershell。但仍可在 Docker 桌面 CLI 工具上访问 - Windows Container Access is denied. (0x5). using Powershell. But still accessible on Docker Desktop CLI tool 如何使用Windows批处理脚本在“,”中查找和替换文本文件中的“=”? - How to find and replace “=” in a text file with “,” using windows batch script? 查找直到该行,然后用另一个文本文件Windows批处理脚本替换 - Find until that line and replace with another text file windows batch script 通过 Windows 批处理文件用文本文件中的 unicode 替换字符串 - Replace string with unicode in text file via Windows batch file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM