Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 450
coderE1 Avatar asked Dec 27 '17 22:12

coderE1


2 Answers

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.

like image 198
Ansgar Wiechers Avatar answered Oct 03 '22 17:10

Ansgar Wiechers


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.

like image 38
mklement0 Avatar answered Oct 03 '22 18:10

mklement0