我有一个输入CSV文件ttt.csv,该文件以逗号分隔,每个字段都可能包含双引号和逗号:
这是ttt.csv的内容:
"CN=Bar\\,Alex,OU=Users,OU=Headquarters,DC=CORP",Bar,Alex,"Barziza,Alex",BARAAA,[email protected]
"CN=Boo\\,Ryan,OU=Users,OU=Headquarters,DC=CORP",Boo,Ryan,"Boo,Ryan",BABBBB,[email protected]
我需要循环此文件,对于每一行,我需要获取6个值中的每个值,并向数据库创建我的SQL插入语句。
在我的情况下,对于2号线,我将需要获得:
Value1= CN=Bar\\,Alex,OU=Users,OU=Headquarters,DC=CORP
Value2= Boo
Value3= Ryan
Value4= Boo,Ryan
Value5= BABBBB
Value6= [email protected]
我使用了包含双引号的定界符,它似乎不起作用:
set str2="CN=Bar\\,Alex,OU=Users,OU=Headquarters,DC=CORP",Bar,Alex,"Barziza,Alex",BARAAA,[email protected]
echo %str2%
for /f "tokens=1 delims=(,")" %%a in ("!str2!") do ( set newstr2=%%a )
echo !newstr2!
正如我在上面评论的那样,只需使用一个普通的for
循环-no /f
,no /r
,no /d
,no /l
,只是一个普通的简单for
循环。它会处理CSV分隔符,同时将引用的内容视为单个令牌。
@echo off
setlocal enabledelayedexpansion
set str2="CN=Bar\\,Alex,OU=Users,OU=Headquarters,DC=CORP",Bar,Alex,"Barziza,Alex",BARAAA,[email protected]
echo %str2%
set idx=0
for %%a in (%str2%) do (
set "newstr[!idx!]=%%~a"
set /a idx += 1
)
set newstr
输出:
C:\ Users \ me \ Desktop> test.bat“ CN = Bar \\,Alex,OU = Users,OU = Headquarters,DC = CORP”,Bar,Alex,“ Barziza,Alex”,BARAAA,aaa @ email。 com
newstr [0] = CN = Bar \\,Alex,OU = Users,OU = Headquarters,DC = CORP
newstr [1] = Bar
newstr [2] = Alex
newstr [3] = Barziza,Alex
newstr [4] = BARAAA
newstr [5] [email protected]
如果您的csv数据包含不应被视为标记定界符的未加引号的空格,则可以在分割之前将空格临时转换为下划线,然后像这样转换回去:
@echo off
setlocal enabledelayedexpansion
set str2="CN=Ryan\\,David Paul,OU=Users,OU=Singapore,DC=GLOBAL,DC=CORP",Ryan,David Paul,"Ryan, David Paul",RPAUL123,[email protected]
echo %str2%
set idx=0
for %%a in (%str2: =_%) do (
set "str=%%~a"
set "newstr[!idx!]=!str:_= !"
set /a idx += 1
)
set newstr
如果愿意,可以阅读有关子字符串替换的更多信息。输出:
C:\ Users \ me \ Desktop> test.bat
“ CN = Ryan \\,David Paul,OU = Users,OU =新加坡,DC = GLOBAL,DC = CORP”,Ryan,David Paul,“ Ryan,David Paul” ,RPAUL123,David @ aaad.com
newstr [0] = CN = Ryan \\,David Paul,OU =用户,OU =新加坡,DC = GLOBAL,DC = CORP
newstr [1] = Ryan
newstr [2] = David Paul
newstr [3] = Ryan,David Paul
newstr [4] = RPAUL123
newstr [5] [email protected]
当然,如果您的数据已经包含下划线,请使用不包含下划线的字符-反引号,波浪号,美元符号或其他符号。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句