我有一些看起来像这样的格式化文本:
5
4294967296个
4
4294967296个
2
4294967296个
2
4294967296个
2
4294967296
我想把它变成一个.csv文件,其中每两行连接在一起,如下所示:
5,4294967296
4,4294967296
2,4294967296
2,4294967296
2,4294967296
如何批量执行此操作?我知道如何读入和读出文件,而我真正需要做的就是将.txt更改为.csv。我只是坚持如何批量合并这些行。
我意识到how to join 2 lines using batch file有点类似,但是这个答案搜索了一个我没有的文本模式。
答案 0 :(得分:1)
我在Linux机器上,所以我无法测试它,但它应该是这样的
@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
)
)
答案 1 :(得分:1)
剧本:
@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"
<强>输出强>:
==>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
==>
资源(必读):
答案 2 :(得分:1)
@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