Board logo

标题: [文本处理] 求助批处理文本截取 [打印本页]

作者: jzy8158    时间: 2023-4-18 15:28     标题: 求助批处理文本截取

有一份txt文件,内容如下
:0200000480106A
:200000000C027936313531353230303033415254303030530A80008000000016D080A40084
:200020000000031F8C801F4000000005E6801F0000000000AA801F2000000000F6802000AA
:20004000000000024880008000000016D080A4000000031F8C801F4000000005E6801F0035
:2000600000000000AA801F4000000005E6801F0000000000AA0000000000000000000000C3
:20008000000000000000000000000000000000000000000000000000000000000000000060
:2000A000000000000000000000000000000000000000000000000000000000000000000040
:2000C000000000000000000000000000000000000000000000000000000000000000000020
:2000E000000000000000000000000000000000000000000000000000000000000000000000
:200100000000000000000000000000000000000000000000000000000000000000000000DF
:02000004801565
:20000000CBA150428FC2504254E3504219045142DD245142A84651426D67514231885142BE
:20002000F6A85142C1CA514285EB51424A0C52420E2D5242D34D52429E6F524262905242DA
:2000400027B15242ECD15242B0F252427B1453423F35534204565342C976534293985342FD
:2000600058B953421DDA5342E1FA5342A61B5442713D5442355E5442FA7E5442BE9F544219
:2000800089C154424EE2544212035542D7235542A2455542666655422B875542F0A7554225
:2000A000B4C855427FEA5542440B5642082C5642CD4C5642986E56425C8F564221B056423F
:2000C000E5D05642B0F256427513574239345742FE545742C97657428D97574252B857424F
:2000E00017D95742E1FA5742A61B58426A3C5842355E5842FA7E5842BE9F584283C0584255
:200100004EE2584212035942D72359429C445942666659422B875942F0A75942BAC9594251
:200120007FEA5942440B5A42082C5A42D34D5A42986E5A425C8F5A4227B15A42ECD15A424E
:20014000B0F25A427B145B423F355B4204565B42C9765B4293985B4258B95B421DDA5B4247
:20016000E7FB5B42AC1C5C42713D5C423B5F5C4200805C42C5A05C428FC25C4254E35C4231
:2001800019045D42E3255D42A8465D426D675D4237895D42FCA95D42C1CA5D428BEC5D4219
:2001A000500D5E421B2F5E42DF4F5E42A4705E426F925E4233B35E42F8D35E42C3F55E42EC
:0200000480205A
:200000000000218004002180080021800C002180100021805408218098102180DC182180B8
:20002000202121802421218028212180942221800024218004242180142B21800000000049
:200040000000000000000000000000000000000000000000000000000000000000000000A0
:20006000000000000000000000000000000000000000000000000000000000000000000080
:20008000000000000000000000000000000000000000000000000000000000000000000060
:2000A000000000000000000000000000000000000000000000000000000000000000000040
:2000C000000000000000000000000000000000000000000000000000000000000000000020
:2000E000000000000000000000000000000000000000000000000000000000000000000000

需要输出:0200000480205A所在行开始的内容到A文本
其他内容输出到B文本

我现在想到的是先找到:0200000480205A所在行,在执行输出操作,但是需要做两次搜索操作,效率较低,
请问有没有只用一次搜索操作就完成需求的代码?
作者: jzy8158    时间: 2023-4-18 15:29

原文件有几万行,这里是精简过的内容
作者: qixiaobin0715    时间: 2023-4-18 15:42

  1. @echo off
  2. for /f "delims=" %%i in (1.txt) do (
  3.     if "%%i"==":0200000480205A" set n=true
  4.     if defined n (
  5.         echo,%%i>>A.txt
  6.     ) else (
  7.         echo,%%i>>B.txt
  8.     )
  9. )
  10. pause
复制代码

作者: jzy8158    时间: 2023-4-18 15:55

回复 3# qixiaobin0715


    可以的,谢谢!
作者: jzy8158    时间: 2023-4-18 16:14

回复 3# qixiaobin0715


    你好请问 只要匹配一行前几个字符,该怎么写条件呢?
作者: Batcher    时间: 2023-4-18 16:30

回复 1# jzy8158


文件比较大的话,推荐试试 sed.exe 这个第三方命令行工具:
http://bcn.bathome.net/tool/4.8/sed.exe
  1. sed "0,/:0200000480205A/!d" "1.txt" | sed "$d" > "A.txt"
  2. sed "/:0200000480205A/,$!d" "1.txt" > "B.txt"
复制代码

作者: Batcher    时间: 2023-4-18 16:30

回复 5# jzy8158


    请举例说明需要匹配哪几个字符
作者: jzy8158    时间: 2023-4-18 16:38

回复 7# Batcher

S325801783A402030251015555555555025001555555555500000000000000000000000000001E
S325801783C40000000000000000000000000000000000000000000000000000000000000000FC
S325801783E40000000000000000000000000000000000000000000000000000000000000000DC
S325801784040000000000000000000000000000000000000001020200000000000016FF0001A0
S325801784240203FFFF04FF0506FFFFFFFFFF0708FFFF09FFFFFFFF0AFFFFFFFFFFFFFFFF0B70
S325801784440CFFFFFF0D0EFFFFFFFFFF0F10FF11FFFFFFFFFFFFFFFFFFFFFF1213FFFFFFFF17
S32580178464FFFF14FFFF04020000000000000000000000000000000000000000000000000045
S3258017848400000000000000000000000000000000000000000000000000000000000000003B
S325801784A400000000000000000000000000000000000000000000000000000000000000001B
S325801784C40000000000000000000000000000000000000000000000000000000000000000FB
S321801784E400000000000000000000000000000000000000000000000000002014AB
S30F801F0280917001E8D9EEE8C9DC0E83
S30F801F02A0917001E8D9EEC8E9DC0E63
S30F801F02C0917001E8D9EEE0E9DC0E2B
S30F801F02E0917001E8D9EEF8E9DC0EF3
S30F801F0300917001E8D9EED0F9DC0EEA
S30F801F0320917001E8D9EEE8F9DC0EB2
S30F801F0360917001E8D9EE322ADC0EF7
S30F801F0380917001E8D9EE263ADC0ED3
S30F801F0C80910001E6D9EE3E00DC0E5E
S30F801F2140917001E8D9EE440ADC0E07
S30F801F2160917001E8D9EE660ADC0EC5
S30F801F2180917001E8D9EE481ADC0EB3
S30F801F21A0917001E8D9EE6A1ADC0E71
S30F801F21C0917001E8D9EE4C2ADC0E5F
S30F801F21E0917001E8D9EE6E2ADC0E1D
S30F801F2200917001E8D9EE503ADC0E0A
S30F801F2220917001E8D9EE723ADC0EC8
S30F801F2640917001E8D9EE241ADC0E12
S30F801F2C60917001E8D9EE3E1ADC0ED2
S30F801F2DC0917001E8D9EE0C3ADC0E83
S30F801F4F00917001E8D9EE182ADC0E25
S325801F60001DFBB4A0000000000000000000000000000000000000000000000000000000006F
S325801F60201DFB1EA100000000000000000000000000000000000000000000000000000000E4
S325801F60401DFB75A1000000000000000000000000000000000000000000000000000000006D
S325801F60601DFB58AB0000000000000000000000000000000000000000000000000000000060
S325801F60801DFB64A1000000000000000000000000000000000000000000000000000000003E
S325801F60A01DFB36A1000000000000000000000000000000000000000000000000000000004C
S325801F60C01DFBAF9E00000000000000000000000000000000000000000000000000000000B6
S30B801F60E01DFB51A000907C
S325801F61001DFB34A000000000000000000000000000000000000000000000000000000000EE
S325801F61201DFB9EA00000000000000000000000000000000000000000000000000000000064
S325801F61401DFBF5A000000000000000000000000000000000000000000000000000000000ED
S325801F61601DFBD8AA00000000000000000000000000000000000000000000000000000000E0
S325801F61801DFBE4A000000000000000000000000000000000000000000000000000000000BE
S325801F61A01DFBB6A000000000000000000000000000000000000000000000000000000000CC
S325801F61C01DFB2F9E0000000000000000000000000000000000000000000000000000000035
S30B801F61E01DFBD19F0090FC
S325801F62001DFBB49F000000000000000000000000000000000000000000000000000000006E
S325801F62201DFB1EA000000000000000000000000000000000000000000000000000000000E3
S325801F62401DFB75A0000000000000000000000000000000000000000000000000000000006C
S325801F62601DFB58AA000000000000000000000000000000000000000000000000000000005F
S325801F62801DFB64A0000000000000000000000000000000000000000000000000000000003D
S325801F62A01DFB36A0000000000000000000000000000000000000000000000000000000004B
S325801F62C01DFBAF9D00000000000000000000000000000000000000000000000000000000B5
S30B801F62E01DFB519F00907B
S325802000000000218004002180080021800C002180100021805408218098102180DC18218012
S32580200020202121802421218028212180942221800024218004242180142B218000000000A3
S325802000400000000000000000000000000000000000000000000000000000000000000000FA
S325802000600000000000000000000000000000000000000000000000000000000000000000DA
S325802000800000000000000000000000000000000000000000000000000000000000000000BA

如上:输出S32580200000所在行之前的内容到A文件,其他内容输出到B文件,在A文件末尾加S7058010020068,
在B文件开头加S00600006C7463B6
作者: Batcher    时间: 2023-4-18 17:03

回复 8# jzy8158


文件比较大的话,推荐试试 sed.exe 这个第三方命令行工具:
http://bcn.bathome.net/tool/4.8/sed.exe
  1. sed "0,/S32580200000/!d" "1.txt" | sed "$s/.*/S7058010020068/" > "A.txt"
  2. sed "/S32580200000/,$!d" "1.txt" | sed "1i\S00600006C7463B6" > "B.txt"
复制代码

作者: jzy8158    时间: 2023-4-18 17:08

回复 9# Batcher


    不用第三方工具该怎么处理呢?
作者: Batcher    时间: 2023-4-18 17:21

回复 10# jzy8158
  1. @echo off
  2. cd /d "%~dp0"
  3. setlocal enabledelayedexpansion
  4. >A.txt type nul
  5. >B.txt echo,S00600006C7463B6
  6. for /f "delims=" %%i in (1.txt) do (
  7.     set "s=%%i"
  8.     if "!s:~0,12!"=="S32580200000" set n=true
  9.     if not defined n (
  10.         >>A.txt echo,%%i
  11.     ) else (
  12.         >>B.txt echo,%%i
  13.     )   
  14. )
  15. >>A.txt echo,S7058010020068
复制代码

作者: jzy8158    时间: 2023-4-19 08:48

回复 11# Batcher


    好的,谢谢




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