Batch file 如何使用批处理脚本解析csv文件?
我是批处理文件的新手。 有人能帮我写一个批处理脚本来解析csv文件吗Batch file 如何使用批处理脚本解析csv文件?,batch-file,csv,for-loop,cmd,Batch File,Csv,For Loop,Cmd,我是批处理文件的新手。 有人能帮我写一个批处理脚本来解析csv文件吗 "Expert Info (Chat/Sequence): GET /?password=Katy HTTP/1.1\r\n","Feb 20, 2014 19:34:46.571807000","b5:54:f4:v7:xo:6l" "Expert Info (Chat/Sequence): GET /?password=Cory HTTP/1.1\r\n","Feb 20, 2014 19:34:51.671167000
"Expert Info (Chat/Sequence): GET /?password=Katy HTTP/1.1\r\n","Feb 20, 2014 19:34:46.571807000","b5:54:f4:v7:xo:6l"
"Expert Info (Chat/Sequence): GET /?password=Cory HTTP/1.1\r\n","Feb 20, 2014 19:34:51.671167000","b5:54:f4:v7:xo:6l"
"Expert Info (Chat/Sequence): GET /?password=Mike HTTP/1.1\r\n","Feb 20, 2014 19:34:57.145898000","b5:54:f4:v7:xo:6l"
"Katy", "2014-02-20", "19:34:46", "b5:54:f4:v7:xo:6l"
"Cory", "2014-02-20", "19:34:51", "b5:54:f4:v7:xo:6l"
"Mike", "2014-02-20", "19:34:57", "b5:54:f4:v7:xo:6l"
并将其转换为另一个csv文件,如下所示:
"Expert Info (Chat/Sequence): GET /?password=Katy HTTP/1.1\r\n","Feb 20, 2014 19:34:46.571807000","b5:54:f4:v7:xo:6l"
"Expert Info (Chat/Sequence): GET /?password=Cory HTTP/1.1\r\n","Feb 20, 2014 19:34:51.671167000","b5:54:f4:v7:xo:6l"
"Expert Info (Chat/Sequence): GET /?password=Mike HTTP/1.1\r\n","Feb 20, 2014 19:34:57.145898000","b5:54:f4:v7:xo:6l"
"Katy", "2014-02-20", "19:34:46", "b5:54:f4:v7:xo:6l"
"Cory", "2014-02-20", "19:34:51", "b5:54:f4:v7:xo:6l"
"Mike", "2014-02-20", "19:34:57", "b5:54:f4:v7:xo:6l"
以下是我写的:
@echo off
FOR /F "tokens=6,8,9,10,11* delims=,? " %%a in (file.csv) do (
set pass=%%a
set month=%%b
set day=%%c
set year=%%d
set sec=%%e
set mac=%%f
echo "%%a" %%b %%c %%d %%e %%f
if %month:~1,10%==Jan set month=01
if %month:~1,10%==Feb set month=02
if %month:~1,10%==Mar set month=03
if %month:~1,10%==Apr set month=04
if %month:~1,10%==May set month=05
if %month:~1,10%==Jun set month=06
if %month:~1,10%==Jul set month=07
if %month:~1,10%==Aug set month=08
if %month:~1,10%==Sep set month=09
if %month:~1,10%==Oct set month=10
if %month:~1,10%==Nov set month=11
if %month:~1,10%==Dec set month=12
echo "%pass:~9,4%", "%year%-%month:~1,10%-%day%", "%sec:~,8%", %mac% >> text.csv)
将一个数据转换为另一个数据(如将月份名称转换为月份编号)的最简单方法是使用:
@ECHO关闭
SETLOCAL
(
对于(q21921051.txt)中的/F“tokens=6,8,9,10,11*delims=,?”%%a(
设置过程=%%a
设置月份=%%b
设置日期=%%c
设置年份=%%d
设置秒=%%e
设置mac=%%f
电话:CALC
)
)>text.csv
后藤:EOF
:计算
如果%month:~1,10%==1月整月=01
如果%month:~1,10%==2月设置月=02
如果%month:~1,10%==Mar集合月=03
如果%month:~1,10%==Apr设置月份=04
如果%month:~1,10%==可以设置month=05
如果%month:~1,10%==Jun set month=06
如果%month:~1,10%==Jul设置月份=07
如果%月份:~1,10%==8月份设置月份=08
如果%month:~1,10%==Sep设置月份=09
如果%month:~1,10%==十月设置月=10
如果%month:~1,10%==11月设置月=11
如果%month:~1,10%==12月设置月=12
回显“%pass:~9,4%”、“%year%-%month%-%day%”、“%sec:~8%”、%mac%
后藤:eof
我在测试中使用了一个名为q21921051.txt
的文件
快到了。您的主要问题是延迟扩展
——在块语句(一系列括号内的语句)
中,解析整个块,然后执行。块内的任何%var%
将在执行块之前,在解析块时被该变量的值替换
因此,当遇到IF
时,将使用%variables%
的值执行IF(somethingelse)
,这同样适用于FOR。。。DO(块)
克服此问题的两种常见方法是:1)使用setlocal enabledelayedexpansion
和!瓦尔
代替%var%
以访问var
的更改值,或2)调用子例程以使用更改的值执行进一步处理
我在这里使用了第二种方法
还请注意(将整个例程括起来)在本例中将使用重定向器->text.csv
重定向其所有输出如果希望追加而不是重新创建文件,则
应该是>
。
我不确定什么是困扰%month:~1,10%
。批处理中的子字符串来自%var:~m,n%
,其中,n
是可选的m
是从字符串开头开始的字符数,如果为负数,则从结尾开始<代码>,n正=返回的最大长度;负=末端位置(以字符为单位);missing=在m
因此,由于
%month%
将包含“2月
,只需%month:~1%
-但我留下了您的原件。请查看for
命令-特别是使用/f
开关。您应该在这里发布问题,这些问题是您在尝试解决问题时遇到的问题,而不是问题本身。很抱歉,刚刚添加了我的代码