需要使用批处理脚本基于空行拆分文件 [英] Need to split the file based on empty line using Batch script
问题描述
当空记录开始时,我需要拆分一个文件,同一文件中包含多个文件结构,需要使用批处理脚本拆分文件.
I need to split one file when ever the empty records starts,File contains multiple file structure in the same file need to split the files using batch script.
重要的是,每个子文件都由空记录分隔
Important, that every sub file is separated by empty record
所以数据看起来像:test.csv
so the data look like: test.csv
sno,employee name,address,location,zip code
1,aaaa,12/34,Hyderabad,500089
2,bbbb,12/35/44,chennai,500079
3,cccc,12/31/11,pune,500069
Cardnumber,cardname,card type,card limit
12345,visa,diamond,10000
2345,master,platinum,50000
Accno,bank name,branch code,Branch location
98765,sbi,23456,hyd
12457,citi,8765,usa
4444,axis,78767,India
,我需要像下面这样的单独文件 Test1.csv
and I need separate file like below Test1.csv
sno,employye name,address,location,zipcode
1,aaaa,12/34,Hyderabad,500089
2,bbbb,12/35/44,chennai,500079
3,cccc,12/31/11,pune,500069
Test2.csv
Test2.csv
Cardnumber,cardname,card type,card limit
12345,visa,diamond,10000
2345,master,platinum,50000
Test3.csv
Test3.csv
Accno,bank name,branch code,Branch location
98765,sbi,23456,hyd
12457,citi,8765,usa
4444,axis,78767,india
我已经尝试过以下脚本,但无法正常工作.
I have tried below script but it is not working as expected.
@echo off & setlocal enabledelayedexpansion
set c=0
for /f "tokens=*" %%a in (test.csv) do (
if "%%a" equ "\n" (
set /a c+=1
>f!c!.csv echo.
) else (
>> f!c!.csv echo %%a
)
)
请帮助我为上述要求编写批处理脚本.在此先感谢
Please help me to write the batch script for the above requirement. Thanks in Advance
推荐答案
for /f
忽略空行.因此,您需要一些技巧来使 not 行为空. find /n
在每行中添加一个行号,因此它不再为空.
for /f
ignores empty lines. So you need a little trick for lines not to be empty. find /n
adds a line number to each line, so it isn't empty any more.
@echo off
setlocal enabledelayedexpansion
set c=0
for /f "tokens=1,* delims=]" %%a in ('type t.txt^|find /n /v ""') do (
REM echo A %%a B %%b
if "%%b" equ "" (
set /a c+=1
>f!c!.csv echo.
) else (
>> f!c!.csv echo %%b
)
)
故障排除中保留了REM
行,但我将其保留在此处,因为它可以帮助您了解发生了什么.
The REM
line is left from troubleshooting, but I left it there because it may help you understand, what's going on.
这篇关于需要使用批处理脚本基于空行拆分文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!