断行的问题 - 循环遍历CSV与批处理文件 [英] Loop through CSV file with batch - line break issue

查看:225
本文介绍了断行的问题 - 循环遍历CSV与批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是扩展到另一个问题(<一个href=\"http://stackoverflow.com/questions/10685599/loop-through-csv-file-with-batch-space-issue/10686203#comment13908064_10686203\">Loop通过CSV文件批处理 - 空间的问题)

This is extension to another question (Loop through CSV file with batch - Space issue)

我有这样的csv文件内容

I have csv file content like this

name,sex,age,description,date
venu,m,16,test mesg,2012-05-01
test,f,22,"He is good guy
and
brilliant",2012-05-01

我使用这个命令循环这个文件。

I am looping this file using this command.

For /F "usebackq tokens=1-3 delims=" %%x in (test.csv) Do (

但由于在第二排线断裂,我收到3条即使有两个记录在文件中。

But since there is line break in second row, I am getting 3 records even though there are two records in the file.

如何解决这一问题?先谢谢了。

How to fix this? Thanks in advance.

推荐答案

主要的问题似乎是计算一个行引号。结果
如果引用的次数为奇数,那么你需要追加下一行并重新计数引号。

The main problem seems to be to count the quotes in a line.
If the count of quotes is odd then you need to append the next line and count again the quotes.

在字符串中的字符计数是有点棘手,如果你不通过所有charachters迭代。结果
我这里使用的延迟减少工艺,每个引号将被替换用途不同一个 +1 和所有其他字符被删除。结果
开始,并终止该行以适当的方式总是有一个额外的 +1 开头,这将是一个 1 在前面。

Counting of characters in a string is a bit tricky, if you won't iterate through all charachters.
I used here the delayed reduction technic, each quote will be effectivly replaced by a +1 and all other characters are removed.
To begin and terminate the line in a proper way there is always one extra +1 at the beginning, which will be compensated by a -1 in front.

主要的窍门是从一个报价恰好与一个 +1 通过替换每个报价!#替换完整的文本到下一:#= 结果。
此工程为#:#= ...&LT;一些文本&GT; ... 总是会扩大到 +1 ,因为变量的含量 +1 等搜索模式无法找到。结果
其他的替代品是只需要避免感叹号问题和脱字符号诠释文本。

The main trick is to replace the complete text from one quote to the next with exactly one +1 by replacing each quote with !!#:#=.
This works as !#:#=...<some text>...! will always be expanded to +1, as the content of the variable # is +1 and so the search pattern # can't be found.
The other replacements are only necessary to avoid problems with exclamation marks and carets int the text.

:::::::::::::::::::::::::::::::::::::::::::
:CountQuotes <stringVar> <result>
setlocal EnableDelayedExpansion
set "line=!%~1!"
set "#=+1"

rem DelayedExpansion: double all quotes
set "line=!line:"=""!"

rem DelayedExpansion: remove all carets ^
set "line=!line:^=!"

rem PercentExpansion: Remove all !
set "line=%line:!=%"

rem PercentExpansion: Replace double quotes to !!#:#=
set "line=-1^!#:#=%line:""=^!^!#:#=%"

for /F "delims=" %%X in ("!line!") do (
    set /a count=%%X!
)

(
    endlocal
    set %~2=%count%
    exit /b
)

和进行追加线和插入换行逻辑

And the logic for appending lines and inserting linefeeds

@echo off
setlocal DisableDelayedExpansion
set "lastLine="
set LF=^


rem Two empty lines
for /F "delims=" %%A in (test.csv) do (
    set "line=%%A"
    setlocal EnableDelayedExpansion
    set "line=!line:\=\x!"
    if defined lastLine (
        set "line=!lastLine!\n!line!"
    )

    call :CountQuotes line quoteCnt
    set /a rest=quoteCnt %% 2
    if !rest! == 0 (
        for %%L in ("!LF!") DO set "line=!line:\n=%%~L!"
        set "line=!line:\\=\!"
        echo Complete Row: !Line!
        echo(
        set "lastLine="
    ) ELSE (
        set "lastLine=!line!"
    )

    for /F "delims=" %%X in (""!lastLine!"") DO (
        endlocal
        set "lastLine=%%~X"
    )
)
exit /b

:::::::::::::::::::::::::::::::::::::::::::
:CountQuotes <stringVar> <result>
setlocal EnableDelayedExpansion
set "line=!%~1!"
set "#=+1"

rem DelayedExpansion: double all quotes
set "line=!line:"=""!"

rem DelayedExpansion: remove all carets ^
set "line=!line:^=!"

rem PercentExpansion: Remove all !
set "line=%line:!=%"

rem PercentExpansion: Replace double quotes to !!#:#=
set "line=-1^!#:#=%line:""=^!^!#:#=%"

for /F "delims=" %%X in ("!line!") do (
    set /a count=%%X!
)

(
    endlocal
    set %~2=%count%
    exit /b
)

这篇关于断行的问题 - 循环遍历CSV与批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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