How to parse csv file using script package?

I am new to batch files. Can someone please help me write a script package 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 into 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)
+3
source share
2 answers
@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 with a name q21921051.txtfor my testing.

Almost there. The main problem - delayed expansion- in a block expression (a parenthesised series of statements), parsing of integers and , then . Any one %var%inside the block will be replaced by the value of the variable at the moment the block is analyzed - before the block is executed.

, IF (something) else (somethingelse) %variables% IF - FOR ... DO (block)

: 1) setlocal enabledelayedexpansion !var! %var% var 2) .

, ( ) - >text.csv. > >>, , .

, %month:~1,10%. %var:~m,n%, ,n ; m - count-of-chars-from-begin-of-string, , . ,n = ; = ; missing = m

, %month% "Feb, , , %month:~1% - .

+2

, , , array:

@echo off
setlocal EnableDelayedExpansion

rem Create the conversion array of month names to month numbers
set m=100
for %%a in (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) do (
   set /A m+=1
   set month["%%a]=!m:~1!
)

(for /F "tokens=6,9,10,11,12,14 delims==,. " %%a in (file.csv) do (
   echo "%%a", "%%d-!month[%%b]!-%%c", "%%e", %%f
)) > text.csv
+2

All Articles