Powershell v2:用 LF 替换 CRLF [英] Powershell v2: Replace CRLF with LF

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

问题描述

使用从批处理文件调用的 Powershell v2,我想用一个 LF 替换文件中的每个 CRLF.如果一个文件只有 LF 而没有任何 CR,那么我希望所有的 LF 都被单独留下.

Using Powershell v2 called from a batch file, I want to replace each CRLF in a file with just an LF. If a file only has LF without any CR, then I want all the LF to be left alone.

如果可能的话,我不想在结果文件中使用终止的 CRLF.

I do not want a terminating CRLF in the resultant file, if possible.

我在 Stack Overflow 上发现 this question here,似乎很接近,但它没有指定 Powershell 版本要求,也没有指定上述其他标准.因此提出了这个问题.

I found this question here on Stack Overflow, that seems to be a close match, but it does not specify a Powershell version requirement, nor does it specify the other criteria above. Hence this question.

该问题的公认答案推荐此代码:

The accepted answer for that question recommends this code:

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

我对其稍作修改,并将其调整为在批处理文件中工作,以读取:

I slightly modified it, and adjusted it to work from within a batch file, to read:

powershell -Command "(Get-Content file1.txt) -join '`n' > file2.txt"

很遗憾,这不起作用.所有 LF 都转换为字符串 `n.

Unfortunately, this does not work. All LF's are converted to the string `n.

我怎样才能让它工作?

推荐答案

我之前的那些是对的,你应该使用 "`n"

Those before me are right you should use "`n"

使用 PowerShell 时,我建议执行以下开关:
-noninteractive 表示您不想与 powershell 交互
-NoProfile - 大大加快速度(跳过加载配置文件)
-ExecutionPolicy Bypass - 如果您在公司环境中,则绕过安全问题

When using PowerShell I recommend executing it the following switches:
-noninteractive indicate you do not want to interact with the powershell
-NoProfile - speeds up the things considerably (skips loading profile)
-ExecutionPolicy Bypass - bypasses security issues if you are on companies environment



很抱歉你提到的错误.我现在拥有 PowerShell 2.0 测试工具.



Sorry about the mistake you mentioned. I now have PowerShell 2.0 testing facility.

修复了您的示例(错误是您必须转义双引号,因为 powershell.exe 会解释它们).这种方法不能完全工作,因为它在文件末尾留下 CRLF:

The fixed your example (the mistake was that you have to escape the double quotes due to the powershell.exe interpreting them). This approach does not work completely as it leaves CRLF at the end of the file:

powershell.exe -noninteractive -NoProfile -ExecutionPolicy Bypass -Command "& {(Get-Content file_crlf.txt) -join "`n" > file_lfonly.txt};"

但是,完全正确的解决方案需要不同的方法(通过 IO.file 类):

However, the completely correct solution needs different approach (via IO.file class):

powershell.exe -noninteractive -NoProfile -ExecutionPolicy Bypass -Command "& {[IO.File]::WriteAllText('file_lfonly.txt', ([IO.File]::ReadAllText('file_crlf.txt') -replace "`r`n", "`n"))};"

这会将您的 CRLF 完全转换为 LF.只是一小部分警告它转换为 ASCII 而不是 Unicode(超出了这个问题的范围).

This completely converts your CRLF to LF. Just small piece of warning it converts to ASCII not Unicode (out of scope of this question).

所有示例现在都在 PowerShell v2.0.50727 上进行了测试.

All examples are now tested on PowerShell v2.0.50727.

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

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