How to remove newlines from a text file with batch or PowerShell

左心房为你撑大大i 提交于 2020-01-24 07:49:14

问题


Essentially, I want to read from file.txt with contents

apple
banana
carrot

and write to newfile.txt so that it will have contents

apple banana carrot

I need to do this on a Windows computer on which I do not have install permissions.

I tried

set row=
for /f %%x in (file.txt) do set row=%row% %%x
echo row > newfile.txt

and I tried using PowerShell statements (I cannot run PowerShell scripts) instead of the CMD-style for loop.

powershell -Command "(Gc file.txt) | Foreach-Object -Process {set row=%row% _$} | Out-File newFile.txt"

but both produce an empty file. Is there a way to do this?


回答1:


Get-Content returns the content of a file as an array of lines with the line breaks already removed, so all you need to do (in PowerShell) is to join the lines and write the result back to a file:

(Get-Content 'input.txt') -join ' ' | Set-Content 'output.txt'

Not recommended, but if you must do this in batch you need something like this:

@echo off
setlocal EnableDelayedExpansion
set row=
for /f %%x in (file.txt) do set "row=!row! %%x"
>newfile.txt echo %row%

Note that delayed expansion is required for this to work. Without it %row% in the loop body would be expanded at parse time (when the variable is still empty), so you'll end up with just the last line from the input file in the variable after the loop completes. With delayed expansion enabled (and using !row! instead of %row%) the variable is expanded at run time, i.e. during the loop iterations as one would normally expect.

For further information on delayed expansion see Raymond Chen's blog.




回答2:


To complement Ansgar Wiechers' helpful answer:

Executing the following command from a batch file / a cmd.exe console window should do what you want:

powershell -command "\"$(Get-Content file.txt)\" > newFile.txt"
  • Note the escaping of embedded " as \", which PowerShell requires when called from the outside (by contrast, PowerShell-internally, ` is the escape character).

  • Enclosing the Get-Content file.txt call - which outputs an array of lines - in a double-quoted string, using subexpression operator $(...), means that the array elements are implicitly joined with a space each.

Note, however, that PowerShell's output-redirection operator, >, creates UTF16-LE ("Unicode") encoded files by default, as does Out-File (at least in Windows PowerShell; the cross-platform PowerShell Core defaults to (BOM-less) UTF-8).

To control the output encoding, use the -Encoding parameter, which you can apply to Out-File or, preferably - knowing that strings are being output - Set-Content.

In Windows PowerShell, note that Set-Content - in contrast with > / Out-File - defaults to the encoding implied by the legacy "ANSI" code page, typically Windows-1252.



来源:https://stackoverflow.com/questions/47999562/how-to-remove-newlines-from-a-text-file-with-batch-or-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!