如何读取逗号分隔的文件并在一行中输出3的结果

时间:2019-01-16 10:14:36

标签: batch-file notepad++

我有一个文本文件,其中包含超过25,000条记录,这些记录在一行中如下所示:

cr:121,cr:122,cr:123,cr:124,cr:221,cr:222,cr:223,cr:224,cr:225,cr:321, and so on..

我想创建一个批处理文件,以读取逗号分隔的值,并以3为批号将其输出到新文件中。

预期结果:newfile.txt

cr:121,cr:122,cr:123
cr:124,cr:221,cr:222
cr:223,cr:224,cr:225
cr:321

我在站点上进行了搜索,但我只能找到批处理命令以获取用于处理多行而不是一行的文件的FOR语句。

我正在尝试

for /f "usebackq tokens=1-4 delims=," %%a in ("algtest_extract.txt") do (
echo %%a %%b %%c %%d )

但这仅适用于多行文件。它给了我前四个值,然后退出。

2 个答案:

答案 0 :(得分:1)

在记事本++中,按Ctrl+H,在"Regular expression"中设置搜索模式,然后使用搜索模式

([^,]+,[^,]+,[^,]+),

并与

进行回复
\1\n

UPD

如果要使用500个元素而不是3个,请使用搜索模式((?:[^,]+,){500})。 可以使用搜索模式,$

替换每行末尾的逗号

答案 1 :(得分:0)

我无法抗拒编写批处理脚本来完成您的任务。给定脚本称为reshape.bat,请提供输入文本文件作为命令行参数,如下所示:

reshape.bat "algtest_extract.txt"

要将输出数据存储到另一个文件中,例如algtest_reshaped.txt,请执行以下操作:

reshape.bat "algtest_extract.txt" > "algtest_reshaped.txt"

这是代码(请参阅所有说明性注释):

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_FILE=%~1" & rem // (file to process; `%~1` means first argument)
set "_COL=3"    & rem // (number of columns in the output data)
set "_SEP=,"    & rem // (separator character for input and output data)
set "_PAD="     & rem // (if defined, fill up last row with empty cells)

rem // Read from file:
< "%_FILE%" call :PROCESS

endlocal
exit /B


:PROCESS
    rem // Initialise variables:
    set "REST=" & rem // (remaining text string after the first separator)
    set "LINE=" & rem // (row collector for line/row to output)
    set "IDX=0" & rem // (column counter for output data)
    setlocal EnableDelayedExpansion
:READ
    rem // Read some text, 1023 characters/bytes at most:
    set "STR=" & set /P STR=""
    rem // Terminate loop if no more data are available:
    if not defined STR goto :END
    rem // Precede with potential remainder from previous loop:
    set "STR=!REST!!STR!"
:PARSE
    rem // Extract everything behind the first separator:
    if defined STR set "REST=!STR:*%_SEP%=!"
    rem // No separator had been found, so read more text:
    if "!REST!"=="!STR!" goto :READ
    rem // Extract part in front of first separator:
    for /F "delims=%_SEP%" %%I in ("ITEM=!STR!") do set "%%I"
    rem // Increment column counter:
    set /A "IDX+=1" & if !IDX! lss %_COL% (
        rem // Output line/row not yet complete, so go on assembling:
        set "LINE=!LINE!%_SEP%!ITEM!"
    ) else (
        rem // Output line/row complete, hence return it:
        echo(!LINE:*%_SEP%=!%_SEP%!ITEM!
        rem // Reset row collector and column counter:
        set "LINE=" & set /A "IDX=0"
    )
    rem // Keep on parsing using the remainder:
    set "STR=!REST!" & goto :PARSE
:END
    rem // Return potential remaining data:
    if defined _PAD (set /A "IDX=_COL-IDX-1") else (set /A "IDX=0")
    set "LINE=!LINE!%_SEP%!REST!"
    for /L %%I in (1,1,%IDX%) do set "LINE=!LINE!%_SEP%"
    if defined LINE echo(!LINE:*%_SEP%=!
    endlocal