使用 powershell 替换 CRLF [英] Replace CRLF using powershell

查看:20
本文介绍了使用 powershell 替换 CRLF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编者注:根据 OP 后来的评论来看,这个问题的要点是:如何在 PowerShell 中将带有 CRLF(Windows 风格)行尾的文件转换为仅 LF(Unix 风格)文件?

Editor's note: Judging by later comments by the OP, the gist of this question is: How can you convert a file with CRLF (Windows-style) line endings to a LF-only (Unix-style) file in PowerShell?

这是我的 powershell 脚本:

Here is my powershell script:

 $original_file ='C:UsersabcDesktopFileabc.txt'
 (Get-Content $original_file) | Foreach-Object {
 $_ -replace "'", "2"`
-replace '2', '3'`
-replace '1', '7'`
-replace '9', ''`
-replace "`r`n",'`n'
} | Set-Content "C:UsersabcDesktopFileabc.txt" -Force

使用此代码,我可以将 2 替换为 3,将 1 替换为 7,将 9 替换为空字符串.我无法仅用换行符替换回车换行符.但这不起作用.

With this code i am able to replace 2 with 3, 1 with 7 and 9 with an empty string. I am unable to replace the carriage return line feed with just the line feed. But this doesnt work.

推荐答案

您尚未指定版本,我假设您使用的是 Powershell v3.

You have not specified the version, I'm assuming you are using Powershell v3.

试试这个:

$path = "C:UsersabcDesktopFileabc.txt"
(Get-Content $path -Raw).Replace("`r`n","`n") | Set-Content $path -Force

编者注:正如 mike z 在评论中指出的那样,Set-Content 附加了一个不受欢迎的尾随 CRLF.验证: 'hi' >t.txt;(Get-Content -Raw t.txt).Replace("`r`n","`n") |设置内容 t.txt;(Get-Content -Raw t.txt).EndsWith("`r`n"),产生 $True.

Editor's note: As mike z points out in the comments, Set-Content appends a trailing CRLF, which is undesired. Verify with: 'hi' > t.txt; (Get-Content -Raw t.txt).Replace("`r`n","`n") | Set-Content t.txt; (Get-Content -Raw t.txt).EndsWith("`r`n"), which yields $True.

请注意,这会将整个文件加载到内存中,因此如果您想处理大文件,您可能需要不同的解决方案.

Note this loads the whole file in memory, so you might want a different solution if you want to process huge files.

更新

这可能适用于 v2(抱歉无处测试):

This might work for v2 (sorry nowhere to test):

$in = "C:UsersabcDesktopFileabc.txt"
$out = "C:UsersabcDesktopFileabc-out.txt"
(Get-Content $in) -join "`n" > $out

编者注:请注意,此解决方案(现在)写入不同的文件,因此不等同于(仍有缺陷的)v3 解决方案.(针对不同的文件是为了避免 Ansgar Wiechers 在评论中指出的陷阱:使用 > truncates 目标文件 before 执行开始).但更重要的是:这个解决方案也附加了一个尾随 CRLF,这可能是不受欢迎的.使用 'hi' > 验证t.txt;(Get-Content t.txt) -join "`n";>t.NEW.txt;[io.file]::ReadAllText((Convert-Path t.NEW.txt)).endswith("`r`n"),产生 $True.

Editor's note: Note that this solution (now) writes to a different file and is therefore not equivalent to the (still flawed) v3 solution. (A different file is targeted to avoid the pitfall Ansgar Wiechers points out in the comments: using > truncates the target file before execution begins). More importantly, though: this solution too appends a trailing CRLF, which may be undesired. Verify with 'hi' > t.txt; (Get-Content t.txt) -join "`n" > t.NEW.txt; [io.file]::ReadAllText((Convert-Path t.NEW.txt)).endswith("`r`n"), which yields $True.

同样对加载到内存的保留.

Same reservation about being loaded to memory though.

这篇关于使用 powershell 替换 CRLF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆