How can I prevent additional newlines with set-content while keeping existing ones when saving in UTF8?

前端 未结 2 1004
北恋
北恋 2021-01-13 20:26

I have a small powershell script which reads a document with UTF8 encoding, makes some replacements in it and saves it back which looks like this:

(Get-Conte         


        
2条回答
  •  没有蜡笔的小新
    2021-01-13 20:49

    To complement Ansgar Wiechers' helpful answer:

    Using Set-Content -NoNewline (PSv5+) is an option, but only if you pass the output as a single string with embedded newlines, which Get-Content -Raw can do:

    (Get-Content -Raw $path) -replace 'myregex', 'replacement' |  
      Set-Content -NoNewline $path2 -Encoding utf8
    

    Note, however, that the semantics of -replace change with the use of -Raw: now a single
    -replace operation is performed on a multi-line string (the entire file contents) - as opposed to line-individual operations with an array as the LHS.

    Also note that -Raw will preserve the trailing-newline-or-not status of the input.

    If you want the line-by-line semantics and/or want to ensure that the output's final line has no trailing newline (even if the input file had one), use Get-Content without -Raw, and then -join:

    (Get-Content $path) -replace 'myregex', 'replacement' -join [Environment]::NewLine |  
      Set-Content -NoNewline $path2 -Encoding utf8
    

    The above uses the platform-appropriate newline character(s) on output, but note that there's no guarantee that the input file used the same.


    As for what you tried:

    As you've observed, Set-Content -NoNewline with an array of strings causes all strings to be concatenated without a separator - unlike what one might expect, -NoNewline doesn't just omit a trailing newline:

     > 'one', 'two' | Set-Content -NoNewline t.txt; Get-Content -Raw t.txt
     onetwo  # Strings were directly concatenated.
    

    Note: Newlines embedded in input strings are preserved, however.

    The reason for the [IO.File]::WriteAllText() approach not resulting in any newlines is different, as explained in Ansgar's answer.

提交回复
热议问题