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

查看:285
本文介绍了使用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:\Users\abc\Desktop\File\abc.txt'
 (Get-Content $original_file) | Foreach-Object {
 $_ -replace "'", "2"`
-replace '2', '3'`
-replace '1', '7'`
-replace '9', ''`
-replace "`r`n",'`n'
} | Set-Content "C:\Users\abc\Desktop\File\abc.txt" -Force

使用此代码,我可以将3替换为2,将7替换为1,将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:\Users\abc\Desktop\File\abc.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") | Set-Content 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:\Users\abc\Desktop\File\abc.txt"
$out = "C:\Users\abc\Desktop\File\abc-out.txt"
(Get-Content $in) -join "`n" > $out

编者注:请注意,此解决方案(现在)写入到不同文件,因此不等同于(仍然有缺陷的)v3解决方案. (Ansgar Wiechers在注释中指出了另一个文件以避免陷阱的麻烦:在开始执行之前,使用> 截断目标文件).不过,更重要的是:此解决方案也追加了不希望出现的尾随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 is 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天全站免登陆