如何使用批处理脚本解析一个CSV文件? [英] how to parse a csv file using batch script?

查看:1864
本文介绍了如何使用批处理脚本解析一个CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新与批处理文件。
有人可以帮我写一个批处理脚本,将解析一个CSV文件看起来像这样:

 专家信息(聊天/顺序):GET /密码=凯蒂HTTP / 1.1 \\ r \\ n,2014年2月20日19:34:46.571807000,B5 :54:F4:V7:XO:6升专家信息(聊天/顺序):GET /密码=科里斯HTTP / 1.1 \\ r \\ n吗?,2014年2月20日19:34:51.671167000,B5:54:F4:V7:XO:6升专家信息(聊天/顺序):GET /密码=麦克HTTP / 1.1 \\ r \\ n吗?,2014年2月20日19:34:57.145898000,B5:54:F4:V7:XO:6升

和把它转化为一个又一个CSV文件看起来像这样:

 凯蒂,2014年2月20日,19点34分46秒,B5:54:F4:V7:XO:6升
科里,2014年2月20日,19点34分51秒,B5:54:F4:V7:XO:6升
迈克,2014年2月20日,19点34分57秒,B5:54:F4:V7:XO:6升

下面是我写的:

 关闭@echo
FOR / F令牌= 6,8,9,10,11 * delims = ,?%%一中(FILE.CSV)做(集通= %%一
设置月= %% b
集天= %%Ç
设置年= %%ð
设置秒= %%Ë
设置MAC = %%˚F
回声%%一个%% b %%ç%% d %%已Ë%%˚F如果每月%:1,10〜==%设定一月月= 01
如果每月%:1,10〜==%设置二月月= 02
如果每月%:1,10〜==%设定三月月= 03
如果每月%:1,10〜==%四月集月= 04
如果每月%:1,10〜==%可能每月设置05 =
如果每月%:1,10〜==%君集月= 06
如果每月%:1,10〜==%七月一套月= 07
如果每月%:1,10〜==%八月集月= 08
如果每月%:1,10〜==%设定九月月= 09
如果每月%:1,10〜==%设定十月月= 10
如果每月%:1,10〜==%集月月= 11
如果每月%:1,10〜==%十二月集月= 12回声%通:〜9.4%,%,比去年% - %月:〜1,10% - %一天%,%秒:〜8%,%MAC%GT;> text.csv)


解决方案

@ECHO OFF
SETLOCAL

FOR / F令牌= 6,8,9,10,11 * delims = ,?%%一中(q21921051.txt)做(
 集通= %%一
 设置月= %% b
 集天= %%Ç
 设置年= %%ð
 设置秒= %%Ë
 设置MAC = %%˚F
 CALL:CALC

)> text.csv
GOTO:EOF
:钙如果每月%:1,10〜==%设定一月月= 01
如果每月%:1,10〜==%设置二月月= 02
如果每月%:1,10〜==%设定三月月= 03
如果每月%:1,10〜==%四月集月= 04
如果每月%:1,10〜==%可能每月设置05 =
如果每月%:1,10〜==%君集月= 06
如果每月%:1,10〜==%七月一套月= 07
如果每月%:1,10〜==%八月集月= 08
如果每月%:1,10〜==%设定九月月= 09
如果每月%:1,10〜==%设定十月月= 10
如果每月%:1,10〜==%集月月= 11
如果每月%:1,10〜==%十二月集月= 12回声%通:〜9.4%,%,比去年% - %,当月% - %一天%,%秒:〜8%,%MAC%GOTO:EOF

我用了一个文件名为 q21921051.txt 我的测试。

几乎没有。你的主要问题是延迟扩展 - 块语句内(一个parenthesised一系列语句)​​,在整个块被解析和然后执行。任何%VAR%块内将在块被解析的时间变量的值的替换 - 之前执行块

因此​​, IF(某事)其他(somethingelse)将使用的值来执行%变量%在当时的如果遇到 - 同样的道理也适用于 FOR ... DO(块)

两种常用的方法来克服这个是:1)使用 SETLOCAL enabledelayedexpansion ,并使用!无功!代替%VAR%访问 VAR 或2)调用子程序的更改值利用变化进行进一步的处理值。

我在这里用第二种方法

另请注意,<大骨节病>( parenthesising整个常规<大骨节病>)将所有重定向使用重定向其输出 - &GT; text.csv 在这种情况下。 &GT; &GT;&GT; 如果你想追加,而不是重新创建该文件。

我不知道的痴迷是与%月:〜1,10%。批量子是从获得%VAR:〜M,N%,其中,N 是可选的; M 是,如果负计数的-字符从 - 开始子串,从结束。 ,N =阳性最大长度返回;从最终字符负=终端位置;缺少=返回所有在 M

因此​​,因为%,当月%将包含二月,所有需要的是%月:〜1% - 但我离开你原来的

I'm pretty new with batch files. Can someone please help me to write a batch script that will parse a csv file that looks like this:

"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"

and turn it to a another csv file that looks like this:

"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"

here is what I wrote:

@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 OFF
SETLOCAL
(
FOR /F "tokens=6,8,9,10,11* delims=,? " %%a in (q21921051.txt) do (
 set pass=%%a
 set month=%%b
 set day=%%c
 set year=%%d
 set sec=%%e
 set mac=%%f
 CALL :CALC
)
)> text.csv
GOTO :EOF
:calc

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%-%day%", "%sec:~,8%", %mac%

GOTO :eof

I used a file named q21921051.txt for my testing.

Nearly there. Your major problem is delayed expansion - within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed.

Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered - the same thing applies to a FOR ... DO (block)

Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion and use !var! in place of %var% to access the changed value of var or 2) to call a subroutine to perform further processing using the changed values.

I've used the second method here

Note also that (parenthesising the entire routine) will redirect all of its output using the redirector - >text.csv in this case. The > should be >> if you wish to append rather than create the file anew.

I'm not sure what the obsession is with %month:~1,10%. Substrings in batch are obtained from %var:~m,n% where ,n is optional; m is count-of-chars-from-beginning-of-string, from end if negative. ,n positive = max length to return; negative = end-position in chars from end; missing=return all after m

Hence, since %month% will contain "Feb, all that is required is %month:~1% - but I left your original.

这篇关于如何使用批处理脚本解析一个CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆