使用批处理作业处理文本文件

时间:2015-11-23 08:30:10

标签: batch-file cmd

美好的一天

我需要将货币放在字段:25:上的帐号后面 货币在字段:62F:上 帐号是无字段:25: 我将如何获取如下所示的文本文件:

{1:}{2:1000U}{4: :20:100798 :25:100798 :28C:1519/1 :60F:C151116TZS0,00 :62F:C151117TZS0,00 -}{5:{CHK:691494CC8019}} {1:}{2:1007N}{3:{108:CBC484}}{4: :20:MTID00222 :25:31691035174 :28C:00222/00002 :60M:C151117CAD0,00 :61:1511D1000,003D3 SND FIRNZAJJ
:61:15117D1182,70S1
:62F:C151117CAD184,36 :64:C151117CAD184,36 -}{5:{CHK:76D55C3FE9DA}}

看起来像这样: 请注意:62F:中字段中的货币并不总是相同。

{1:}{2:1000U}{4: :20:100798 :25:100798USD :28C:1519/1 :60F:C151116USD0,00 :62F:C151117USD0,00 -}{5:{CHK:691494CC8019}} {1:}{2:1007N}{3:{108:CBC484}}{4: :20:MTID00222 :25:31691035174 :28C:00222/00002 :60M:C151117CAD0,00 :61:1511D1000,003D3
:61:15117D1182,70S1
:62F:C151117CAD184,36 :64:C151117CAD184,36 -}{5:{CHK:76D55C3FE9DA}}

谢谢

现有代码:

echo off 
SET TempFile=Temp.txt 
findstr ":25:1234" test.txt >> result1.txt 
findstr /n ":25:1234" test.txt >> result2.txt 
findstr /i ":62M: \.:62F:" test.txt >> currency.txt 
FOR /F "tokens=1 delims=:" %%G IN (result2.txt) DO echo %%G >>result3.txt 
pause   

这里有更多相关数据:{1:F01FIRNZAJJACLS0991710374}{2:O9500614151118CLSBUS33AXXX12605310901511181314N}{3:{108:S201511182092336}}{4: :20:S201511182092336 :25:100798 :28C:46/1 :60F:C151118JPY0, :61:1511181118D805000000,NFEXO201511180000023//V2015111800B3B00 :61:1511181118D1373000000,NFEXFXTMAV68177721A//V201511180084C07 :61:1511181118C2178000000,NFEXFXTMAV68135827B//V201511180053B5F :62F:C151118JPY0, -}{5:{CHK:D18476DD1D81}} {1:F01FIRNZAJJAXXX0991711421}{2:O9502036151118MHCBJPJTBXXX47427596761511181336N}{4: :20:ST 1118/0685010 :25:0685010 :28C:00216/001 :60F:C151118JPY4945624, :61:1511181118CY26635000,NTRFNONREF//TRF062/363192 B/O FIRSTRAND BANK LTD :61:1511181118DY2500,NTRFS657DRTSF1807511//TCT023/694154 :61:1511181118DY13328,NTRFS657DRTSF1811632//TCT023/697442 :61:1511181118DY26657466,NTRFS657DRTSF1811632//TCT023/697442 I/O CITIBANK JAPAN LTD :62F:C151118JPY4907330, -}{5:{CHK:347C4ABB2833}} {1:F01FIRNZAJJACLS0991711520}{2:O9500637151118CLSBUS33DXXX10955316551511181337N}{3:{108:S201511182092379}}{4: :20:S201511182092379 :25:100798 :28C:50/1 :60F:C151118AUD0, :61:1511181118D2800000,NFEXFXTMAV68137396B//V20151118004496D :61:1511181118C300000,NFEXFXTSDA68142044//V20151118002A3F7 :61:1511181118C500000,NFEXFXTSDA68131969//V2015111800616AC :61:1511181118C1000000,NFEXFXTSDA68121455//V2015111800A79DC :61:1511181118C1000000,NFEXFXTJGB68136823//V20151118008D7DE :62F:C151118AUD0, -}{5:{CHK:DFE6CC369EED}} {1:F01FIRNZAJJACLS0991711940}{2:O9500645151118CLSBUS33AXXX12605315171511181345N}{3:{108:S201511182092422}}{4: :20:S201511182092422 :25:100798 :28C:48/1 :60F:C151118HKD0, :62F:C151118HKD0, -}{5:{CHK:4E9374364F7E}} {1:F01FIRNZAJJACLS0991712647}{2:O9500701151118CLSBUS33BXXX11318045431511181401N}{3:{108:S201511182092465}}{4: :20:S201511182092465 :25:100798 :28C:48/1 :60F:C151118SGD0, :61:1511181118D1300000,NFEXFXTMAV68178024A//V20151118008388F :61:1511181118C100000,NFEXA151118000037081//V2015111800000D3 :61:1511181118C1200000,NFEXFXTMAV68138706B//V201511180074DB7 :62F:C151118SGD0, -}{5:{CHK:34212515C8D6}} {1:F01FIRNZAJJACLS0991712811}{2:O9500702151118CLSBUS33AXXX12605316791511181402N}{3:{108:S201511182092508}}{4: :20:S201511182092508 :25:100798 :28C:48/1 :60F:C151118KRW0, :62F:C151118KRW0, -}{5:{CHK:4E90352A03A6}} {1:F01FIRNZAJJAXXX0991713343}{2:O9502113151118BOTKJPJTAXXX19937001311511181414N}{4: :20:216-00002 :25:653-0414247 :28C:216/00002 :60M:C151118JPY1585509238, :61:151118D743180,NTRFS6573BCBC2471469//837-5701627 OUR CHG/COMM JPY2500, :61:151118D843720,NTRF25-255840//837-5309398 :61:151118D1061500,NTRFSD3GR4K095LR2UQ1//837-5701839 :61:151118D1396000,NTRF25-255854//837-5309389 :61:151118D1960000,NTRF25-255842//837-5309388 :61:151118D2000000,NTRF25-255838//837-5309397 :61:151118D2577000,NTRF25-255488//837-5308764 :61:151118D26635000,NTRFS480A5502//962BTP183560 MIZUHO BANK, LTD.. HEAD OFFICE (TO :61:151118D826936308,NTRFS6573B8BE5198313//962BTP182113 SMBC TOKYO INTER'L BUSINESS OPER. :62F:C151118JPY721356530, :64:C151118JPY721356530, -}{5:{CHK:72646A6E1228}}

1 个答案:

答案 0 :(得分:0)

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: remove variables starting $
FOR  /F "delims==" %%c In ('set $ 2^>Nul') DO SET "%%c="

SET /a count=0
FOR /f "delims=" %%a IN (q33866504.txt) DO (
 SET "$!count!=%%a"
 IF "%%a"=="-}" (
  SET "currency="
  FOR /l %%c IN (0,1,!count!) DO IF "!$%%c:~0,3!"==":62" SET "currency=!$%%c:~6!"
  FOR %%c IN (0 1 2 3 4 5 6 7 8 9 "," :) DO SET "currency=!currency:%%~c=!"
  FOR /l %%c IN (0,1,!count!) DO (
   IF "!$%%c:~0,3!"==":25" (ECHO !$%%c!!currency!) ELSE (ECHO !$%%c!)
  )
  FOR  /F "delims==" %%c In ('set $ 2^>Nul') DO SET "%%c="
  SET /a count=0
 ) ELSE (SET /a count+=1)
) >u:\newfile.txt

GOTO :EOF

我使用了一个名为q33866504.txt的文件,其中包含我的测试数据。

制作u:\ newfile.txt

您没有说明如何提取货币符号,也没有解释“62”业务。你似乎想在“62F”旁边选择“62M”。

意识到此代码使用delayedexpansion因此!var!指的是变量在循环内变化时的值。

第一步是清除环境中的所有$变量。它们不太可能存在,但这确保了。程序运行后,环境将恢复到原始状态,否则无关紧要。

为遇到的每一行设置环境变量$!count!,因此第一行将转到$0,然后转到$1等。

如果读取的行 -},则递增count(因此每个新行都安装在下一个$变量中。)

如果读取的行 -},则将currency设置为[$变量的第6个字符开头(从“字符0”开始)其中:62为从第0个字符开始的3个字符。

你没有说如何提取货币字符串,所以我选择删除所有数字, currency的字符串中的kbd>

然后再次循环遍历所有$变量。重现每个字符,但如果前3个字符为:25,则将currency的值附加到该行。

然后清除$个变量并将count设置为0,为下一个块做好准备。