帮助写一个批处理脚本解析CSV文件和输出的文本文件 [英] Help in writing a batch script to parse CSV file and output a text file
问题描述
我奋力写一个批处理脚本可以读一个CSV文件,如下面
名称:,市:,国家:
马克,英国伦敦
本,法国巴黎
汤姆,雅典,希腊
有将在CSV文件中的标题行。它应该输出到下面的文本文件:
姓名:Mark
城市:伦敦
国家:英国名称:Ben
城市:巴黎
国家:法国姓名:汤姆
城市:雅典
国家:希腊
字段分隔符(:)在上面的产量预计在标题行本身来提供。因此,所有我需要做的是并置字段标题和它的值。
在此CSV文件的列数不固定,所以该脚本不应限制到3令牌。请帮助!
@ECHO OFF
IF%〜1==GOTO:EOF
SET文件名=%〜1
SET FCOUNT = 0
SET LINENUM = 0
FOR / F标记有usebackq = 1 10 delims =%%一个IN(%文件名%)DO ^
CALL:进程 %%一个%% B%% C%% d的%% E%% F%% G%% H %% i的%% J
GOTO:EOF:修剪
SETTMP =%〜1
:trimlead
如果不是%TMP:〜0.1%==GOTO:EOF
SETTMP =%TMP:〜1%。
GOTO trimlead:处理
SET / A LINENUM + = 1
IF%LINENUM%==1GOTO picknamesSET IND = 0
:显示
IF%FCOUNT%==%IND%(ECHO&GOTO:EOF)
SET / A IND + = 1
CALL:修剪1%
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO!F%IND%!TMP!
ENDLOCAL
转移
GOTO显示器:picknames
IF%1 ==GOTO:EOF
CALL:修剪1%
SET / A FCOUNT + = 1
SETF%FCOUNT%=%TMP%
转移
GOTO picknames
这一批素文字:
-
接受一个参数,该文件以处理的名称;
-
不验证的presence
:
在头标记的结束,并显示值的时候都相应后立即放置头标记; -
修剪所有前导空格(而不是尾随的);
-
考虑的第一行是头行,这也定义令牌在随后的行处理的数量;
-
最多支持10个代币,两区以粗体斜体字突出显示的负责人认为(所以当你需要改变的最大数量,修改这两个方面:如果增加的数量,那么您必须扩展
%%一个%% b%% C...
列表,同样,如果你减少了号码,然后缩小列表)。
I am struggling to write a batch script which can read a CSV file such as below
Name:, City:, Country:
Mark, London, UK
Ben, Paris, France
Tom, Athens, Greece
There will be a heading row in the CSV file. It should output to a text file as below:
Name:Mark
City:London
Country:UK
Name:Ben
City:Paris
Country:France
Name:Tom
City:Athens
Country:Greece
The field separator (:) in the above output is expected to be provided in the header row itself. So all that I need to do is concatenate the field heading and its value.
The number of columns in this CSV file is not fixed, so the script should not limit to 3 tokens. Kindly help!
@ECHO OFF
IF "%~1"=="" GOTO :EOF
SET "filename=%~1"
SET fcount=0
SET linenum=0
FOR /F "usebackq tokens=1-10 delims=," %%a IN ("%filename%") DO ^
CALL :process "%%a" "%%b" "%%c" "%%d" "%%e" "%%f" "%%g" "%%h" "%%i" "%%j"
GOTO :EOF
:trim
SET "tmp=%~1"
:trimlead
IF NOT "%tmp:~0,1%"==" " GOTO :EOF
SET "tmp=%tmp:~1%"
GOTO trimlead
:process
SET /A linenum+=1
IF "%linenum%"=="1" GOTO picknames
SET ind=0
:display
IF "%fcount%"=="%ind%" (ECHO.&GOTO :EOF)
SET /A ind+=1
CALL :trim %1
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO !f%ind%!!tmp!
ENDLOCAL
SHIFT
GOTO display
:picknames
IF %1=="" GOTO :EOF
CALL :trim %1
SET /a fcount+=1
SET "f%fcount%=%tmp%"
SHIFT
GOTO picknames
This batch scipt:
accepts one parameter, the name of the file to process;
does not verify the presence of
:
at the end of a header token, and when the values are displayed they are placed immediately after the corresponding header tokens;trims all the leading spaces (but not the trailing ones);
considers the first row to be the header row, which also defines the number of tokens to process in subsequent rows;
supports up to 10 tokens, and the two areas highlighted in bold italics are responsible for that (so when you need to change the maximum number, modify both areas: if you increase the number, you must expand the
"%%a" "%%b" "%%c" …
list, and, likewise, if you decrease the number, then shrink the list).
这篇关于帮助写一个批处理脚本解析CSV文件和输出的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!