Reputation: 376
I'm have some formatted text that looks like this:
5
4294967296
4
4294967296
2
4294967296
2
4294967296
2
4294967296
I want to make this into a .csv file where every two lines are joined together, like this:
5,4294967296
4,4294967296
2,4294967296
2,4294967296
2,4294967296
How can I do this in batch? I know how to read in and out files, and all I really need to do is change .txt to .csv. I'm just stuck on how to combine the lines in batch.
I realize how to join 2 lines using batch file is kinda similar, but that answer searched for a text pattern, which I don't have.
Upvotes: 0
Views: 688
Reputation: 67206
@echo off
setlocal
call :sub < input.txt > output.csv
goto :EOF
:sub
set /P "line="
:nextPair
set /P "line=%line%,"
set /P "line=%line%"
echo/
if "%line:~8%" equ "" goto nextPair
exit /B
Upvotes: 1
Reputation: 13912
I'm on a Linux machine, so I can't test it but it should be something like this
@echo off
SETLOCAL enableextensions enabledelayedexpansion
set COUNT=0
set even_odd = 1
for /f %%c in ("file.txt") do (
set /A count=%count% + 1
set /A even_odd=%count% %% 2
:: First line is odd (1), ever other is even (0)
if "%even_odd%" == "1" (
echo %%c>> file.csv
echo ,>> file.csv
:: Odd line: Write the line to the CSV, plus a comma
) else (
echo %%c>> file.csv
echo.>> file.csv
:: Even line: Write the line to the CSV, plus a line break
)
)
Upvotes: 1
Reputation: 30103
The script:
@ECHO OFF
SETLOCAL enableextensions enabledelayedexpansion
set "file=D:\bat\files\30356343"
set "line="
(for /F "usebackq tokens=*" %%G in ("%file%.txt") do (
if defined line (
echo !line!,%%G
set "line="
) else (
set "line=%%G"
)
))>"%file%.csv"
rem show results
ECHO ON
type "%file%.txt"
type "%file%.csv"
Output:
==>type "D:\bat\files\30356343.txt"
5
4294967296
4
4294967297
2
4294967298
2
4294967299
==>type "D:\bat\files\30356343.csv"
5,4294967296
4,4294967297
2,4294967298
2,4294967299
==>
Resources (required reading):
Upvotes: 1