找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 28043|回复: 2

[文本处理] [分享]批处理按照指定条件合并行

[复制链接]
发表于 2019-8-31 11:28:04 | 显示全部楼层 |阅读模式
问题:
几行混成一行,第一行是1234567第二行是891011121314第三行是151617181920第四行是21222324252627
第五行是28293031323334第六行是353637383940就是说每3行是776这样排,有听懂的帮我做一下

代码:
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set "FileOld=1.txt"
  4. set "FileNew=2.txt"
  5. set RowNum=1
  6. set ColumnNum=0
  7. (for /f "delims=" %%i in ('type "%FileOld%"') do (
  8.     set /p =%%i<nul
  9.     set /a ColumnNum+=1
  10.     if !RowNum! equ 3 (
  11.         if !ColumnNum! equ 6 (
  12.             echo,
  13.             set ColumnNum=0
  14.             set /a RowNum+=1
  15.             set RowNum=0
  16.         )
  17.     ) else (
  18.         if !ColumnNum! equ 7 (
  19.             echo,
  20.             set ColumnNum=0
  21.             set /a RowNum+=1
  22.         )
  23.     )
  24. ))>"%FileNew%"
复制代码
发表于 2019-8-31 12:19:00 | 显示全部楼层
思路新奇的 看不懂
发表于 2019-9-1 14:08:24 | 显示全部楼层
本帖最后由 a20150604 于 2019-9-1 14:12 编辑

回复 1# Batcher
  1. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2. ::
  3. :: problem: http://www.bathome.net/thread-53773-1-1.html
  4. :: 问题:
  5. :: 几行混成一行,第一行是1234567第二行是891011121314第三行是151617181920第四行是21222324252627
  6. :: 第五行是28293031323334第六行是353637383940就是说每3行是776这样排,有听懂的帮我做一下
  7. ::
  8. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

  9. @echo off
  10. setlocal enabledelayedexpansion
  11. set "RULE=7 7 6"
  12. set "FileOld=1.txt"
  13. set "FileNew=2.txt"

  14. REM make FileOld
  15. >%FileOld% ( for /L %%i in (1 1 100) do (echo;%%i) )

  16. set /a "cycle=%RULE: =+%"

  17. set /a "sum=0,cnt=1"
  18. for %%a in (%RULE%) do (
  19.     set /a "p_!cnt!=(sum+=%%a)-1,cnt+=1"
  20. )

  21. set /a cnt-=1
  22. set "_test="
  23. for /L %%i in (1 1 %cnt%) do (
  24.     REM debug info
  25.     REM echo;%%i;!p_%%i!
  26.     set "_test=!_test!NOT(#-!p_%%i!)^|"
  27. )
  28. set "_test="(!_test:~0,-1!)""

  29. setlocal DISABLEDELAYEDEXPANSION
  30. set "_test="test=%_test:NOT=!%""
  31. REM debug info
  32. REM set _test

  33. setlocal enabledelayedexpansion

  34. >%FileNew% cd.
  35. set "output_line="
  36. set /a "line_ind=-1" & rem 首行记为 第 0 行

  37. >%FileNew% (
  38.     for /f "delims=" %%a in (%FileOld%) do (
  39.         set "output_line=!output_line!%%a"

  40.         set /a "line_ind_in_cycle=(line_ind+=1) %% cycle, !_test:#=line_ind_in_cycle!"
  41.         REM debug info
  42.         REM echo;%%a;!line_ind_in_cycle!;!test!
  43.         if !test!==1 (
  44.             echo;!output_line!
  45.             set "output_line="
  46.         )
  47.     )
  48. )
  49. start "" "%FileNew%"
  50. <nul set /p=Press any key to exit...
  51. >nul pause

复制代码

评分

参与人数 1技术 +1 收起 理由
flashercs + 1 感谢分享

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-19 08:47 , Processed in 0.018232 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表