This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
192} Concatenation - How to convert a text file's column of words into a row of words?
Assume the following test data in the file "MyColumnListOfWords.text"
Hello
World!
This
is
a
test
Run a PowerShell-aided cmd.exe commad-line script:
@echo off & setlocal enableextensions
set source_=MyColumnListOfWords.text
set target_=MyNewRowOfWords.text
powershell -command "(Get-Content '%source_%') -join ' '" > "%target_%"
type "%target_%"
endlocal & goto :EOF
The output is
C:\_D\TEST>cmdfaq
Hello World! This is a test
For another solution get a UNIX utility tr for Windows command-line
(let's call it
unxtr for distinction)
from
UnxUpdates.zip
and the same goes for [unx]sed.exe
@echo off & setlocal enableextensions
set source_=MyColumnListOfWords.text
set target_=MyNewRowOfWords.text
unxtr "\015\012" " " < "%source_%" | unxsed "s/ / /g" > "%target_%"
type "%target_%"
endlocal & goto :EOF
The output is
C:\_D\TEST>cmdfaq
Hello World! This is a test
In the above \015\012 octals correspond to \r and \n (return and
newline). Sed is used to filter the double blanks between the words.
Then there is an ordinary command-line solution. A bit heavy and with
the familiar poison character weaknesses. Note how the explanation
mark(!) is missing from the output.
@echo off & setlocal enableDelayedexpansion
set "line="
for /f "usebackq delims=" %%f in ("MyColumnListOfWords.text") do (
set "line=!line! %%f")
echo !line:~1!
endlocal & goto :EOF
The output is
C:\_D\TEST>cmdfaq
Hello World This is a test
Also a spreadsheet solution could be worked out, but that is beyond
the target area of this collection.