You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Windows命令行将单行数据拆分为每行3个元素的多行?

Windows命令行实现每行拆分3个元素的方法

完全可以通过Windows自带的命令行工具实现,无需外部工具,以下提供两种可行方案:

方案1:使用CMD批处理

如果你的数据是单行输入,或者保存在input.txt文件中,可使用以下批处理脚本:

@echo off
setlocal enabledelayedexpansion
set "count=0"
set "line="
:: 直接处理单行数据的写法
for %%a in (391 896 635 702 041 635 027 177 495) do (
    set /a count+=1
    set "line=!line! %%a"
    if !count! equ 3 (
        echo !line:~1!
        set "count=0"
        set "line="
    )
)

:: 若要处理文件内容,替换上面的for循环为以下内容
:: for /f "tokens=*" %%b in (input.txt) do for %%a in (%%b) do (
::     set /a count+=1
::     set "line=!line! %%a"
::     if !count! equ 3 (
::         echo !line:~1!
::         set "count=0"
::         set "line="
::     )
:: )

方案2:使用PowerShell单行命令

PowerShell处理文本更简洁,单行命令即可完成拆分:

:: 处理单行数据
"391 896 635 702 041 635 027 177 495" -split ' ' | ForEach-Object -Begin { $i=0; $chunk=@() } -Process { $chunk += $_; $i++; if ($i -eq 3) { $chunk -join ' '; $i=0; $chunk=@() } }

:: 处理文件内容
Get-Content input.txt | ForEach-Object { $_ -split ' ' | ForEach-Object -Begin { $i=0; $chunk=@() } -Process { $chunk += $_; $i++; if ($i -eq 3) { $chunk -join ' '; $i=0; $chunk=@() } } }

两种方案均依赖Windows自带工具,能将输入的一行数据按每3个元素拆分为独立行,无需Excel或外部软件。

内容的提问来源于stack exchange,提问作者Hari Krishna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 20:19:59