简体   繁体   English

Powershell外文件强制行尾字符

[英]Powershell Out-File force end of line character

I discovered that I could force a Unicode file to ASCII using the script below, which is really great. 我发现可以使用以下脚本将Unicode文件转换为ASCII,这确实很棒。 I assume it's based on my environment or Windows default, but it's adding a CR and LF at the end of each line. 我假设它基于我的环境或Windows默认设置,但是在每行的末尾添加了CR和LF。 Is there a way to force just a LF character rather than both without loading the entire file into memory? 有没有办法在不将整个文件加载到内存的情况下仅强制使用LF字符,而不是同时强制两个字符? I have seen some solutions that load the entire file into memory and basically do a string replace, which won't work because some of my files are multiple GB. 我已经看到一些将整个文件加载到内存中并基本上执行字符串替换的解决方案,该解决方案将不起作用,因为我的某些文件为多个GB。

Thanks! 谢谢!

get-content -encoding utf8 $inputFile | Out-file -force -encoding ASCII $outputFile

I suggest you use .NET System.File.IO classes from within your script. 我建议您在脚本中使用.NET System.File.IO类。 In particular the System.File.IO.StreamWriter class has a property, NewLine which you can set to whatever characters you want the line terminator characters to be. 特别是System.File.IO.StreamWriter类具有NewLine属性,您可以将其设置为希望将行终止符设置为任何字符。 (Although to be readable by StreamReader the line terminator chars must be \\n or \\r\\n (in C/C++ notation because of conflict with SO and PS on backtick)). (尽管StreamReader可以读取,但行终止符char必须为\\n\\r\\n (在C / C ++表示法中,因为与反引号上的SO和PS冲突))。

Secondary benefit of using IO.StreamWriter, according to this blog is much better perf. 根据此博客的介绍,使用IO.StreamWriter的次要好处是更好的性能。

Basic code flow is something like this (not tested): 基本代码流是这样的(未经测试):

# Note that IO.StreamWriter will use process's current working directory,
#  not PS's. So safer to specify full paths
$inStream =  [System.IO.StreamReader] "c:\temp\orig.txt"
$outStream = new-object System.IO.StreamWriter "c:\temp\copy.txt",  
                                               [text.encoding]::ASCII
$outStream.NewLine = '`n'
while (-not $inStream.endofstream) {
  $outStream.WriteLine( $instream.Readline())
}
$inStream.close()
$outStream.close()

This script should have constant memory requirements, but hard to know what .NET might do under the covers. 该脚本应具有恒定的内存要求,但很难知道.NET可能在幕后做什么。

Can't comment, and apparently the edit isn't enough characters, but it's worth pointing out the string literal usage in 无法评论,显然编辑不够字符,但是值得指出的是字符串字面量用法

$outStream.Newline = '`n'

backfires, because it passes 适得其反,因为它过去了

`n

instead of the newline character itself to $outStream . 而不是$outStream的换行符本身。 Should be: 应该:

$outStream.Newline = "`n"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM