Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine multiple lines in a single text file into one line, in Windows?

I have a multiple standard text files that follow this format, with varying numbers of lines in each file:

Line1
Line2
Line3
Line4

I want to merge every line into one, with a space in between each set of characters, so the text file would look as such:

Line1 Line2 Line3 Line3

...and so on. This needs to work with any given number of lines, due to the fact that each text file contains a different number of lines. My intention is not to merge the lines in the text files; I want each text file to remain separate. All the solutions I have found online either don't quite fit this or work exclusively with UNIX. I am running Windows 7. This can be done in Powershell, VBS, Batch, a particular program, doesn't matter, it just needs to work with Windows.

Much appreciated!

like image 741
WafflesAteMyMother Avatar asked Mar 17 '13 09:03

WafflesAteMyMother


People also ask

How do I combine all lines in a text file into a single line?

The paste Command The paste command just does one thing: Merge lines of files. It's exactly what we need to solve our problems.

How do I combine text in Notepad?

Open the two files you want to merge. Select all text (Command+A/Ctrl+A) from one document, then paste it into the new document (Command+V/Ctrl+V). Repeat steps for the second document. This will finish combining the text of both documents into one.


2 Answers

@ECHO OFF
setlocal
(SET var=)
FOR /f "delims=" %%x IN (list.txt) DO (
CALL SET var=%%var%% %%x
)
SET var=%var:~1%
echo var=%var%=

Where list.txt is the file containing your lines and var is the variable into which you want the lines concatenated.

like image 117
Magoo Avatar answered Nov 03 '22 21:11

Magoo


Using batch:

for /f "usebackqdelims=" %%i in ("infile.txt") do @<nul set /p"=%%i ">>"outfile.txt"
>>"outfile.txt" echo.
like image 40
Endoro Avatar answered Nov 03 '22 22:11

Endoro