Board logo

标题: [文本处理] 求助批处理通过关键字符拆分txt [打印本页]

作者: bryant2009    时间: 2022-12-9 10:25     标题: 求助批处理通过关键字符拆分txt

单一个txt,例如,通关键字“,面,”拆分成n个txt,
根据拆分前后顺序,自动排序。

例子
拆分之前:
5,174.7137,1,001,面,,3,,@
1,1,482492.368,415664.304
2,1,482469.182,415627.564
3,1,482489.587,415563.972
4,1,482491.284,415561.571
5,1,482494.570,415555.573
3,851.6603,2,002,面,,2,,@
1,1,483942.849,417784.072
2,1,483941.546,417784.921
3,1,483941.745,417769.202

拆分之后:
xx-1.txt
5,174.7137,1,001,面,,3,,@
1,1,482492.368,415664.304
2,1,482469.182,415627.564
3,1,482489.587,415563.972
4,1,482491.284,415561.571
5,1,482494.570,415555.573
xx-2.txt
3,851.6603,2,002,面,,2,,@
1,1,483942.849,417784.072
2,1,483941.546,417784.921
3,1,483941.745,417769.202

谢谢各位大大,感谢。
作者: hfxiang    时间: 2022-12-9 10:38

  1. 5,174.7137,1,001, 面,,3,,@
  2. 1,1,482492.368,415664.304
  3. 2,1,482469.182,415627.564
  4. 3,1,482489.587,415563.972
  5. 4,1,482491.284,415561.571
  6. 5,1,482494.570,415555.573
  7. 3,851.6603,2,002, 面,,2,,@
  8. 1,1,483942.849,417784.072
  9. 2,1,483941.546,417784.921
  10. 3,1,483941.745,417769.202
复制代码
以ANSI编码格式保存为1.txt
下载gawk( http://bcn.bathome.net/tool/4.1.0/gawk.exe
执行
  1. gawk "/, 面,/{++n}{print $0>\"xx-\"n\".txt\"}" 1.txt>con
复制代码
结果( xx-1.txt )
  1. 5,174.7137,1,001, 面,,3,,@
  2. 1,1,482492.368,415664.304
  3. 2,1,482469.182,415627.564
  4. 3,1,482489.587,415563.972
  5. 4,1,482491.284,415561.571
  6. 5,1,482494.570,415555.573
复制代码
结果( xx-2.txt )
  1. 3,851.6603,2,002, 面,,2,,@
  2. 1,1,483942.849,417784.072
  3. 2,1,483941.546,417784.921
  4. 3,1,483941.745,417769.202
复制代码

作者: bryant2009    时间: 2022-12-9 11:18

回复 2# hfxiang


    谢谢大大
作者: 77七    时间: 2022-12-9 12:16

  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set /a "n=0"
  4. for /f "tokens=*" %%a in (1.txt) do (
  5.     echo "%%a" | findstr ",面," && ( set /a "n=n+1")
  6.     echo %%a>>xx-!n!.txt
  7. )
  8. pause
复制代码

作者: bryant2009    时间: 2022-12-9 15:28

回复 4# 77七


    大神,厉害啊,这个可以,是慢了一点,但是代码没有问题,感谢




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2