Shell 使用bat文件提取行时出错
在我的文本文件中有这样的行Shell 使用bat文件提取行时出错,shell,batch-file,window,Shell,Batch File,Window,在我的文本文件中有这样的行 @echo off for /f "tokens=* delims= " %%f in (myfile) do ( set line=%%f call :processToken ) goto :eof :processToken for /f "tokens=1* delims=/" %%a in (%line%) do ( echo Got one token: %%a set line=%%b ) if not "%li
@echo off
for /f "tokens=* delims= " %%f in (myfile) do (
set line=%%f
call :processToken
)
goto :eof
:processToken
for /f "tokens=1* delims=/" %%a in (%line%) do (
echo Got one token: %%a
set line=%%b
)
if not "%line%" == "" goto :processToken
goto :eof
因此,当我看到for/f“tokens=1*delims=/”%%a in(%line%)do(
,ECHO打开时,%line%显示为“test a”“test b”“testdim”
)
我不知道为什么会缺少逗号,但我需要这些逗号,以便我可以使用
“delims=,”
从每行中获取这些值。试试这个,添加一些双引号:
"test a","test b","testdim"
"Line 2", "test d","testdin"
有点过火-在“
for..%%A..
中添加的引号部分解决了问题,但第二行包含不平衡的引号-for
不知道哪些引号是哪个,并且认为这是不可能的。(抱歉…)像这样的批量字符串处理是有限的-最好使用为该作业设计的工具。最后,我建议如果不是“%line%”
最好写为如果定义了行
-但是如果您的数据或处理产生了不平衡的引号…文本解析,最好使用grep
或@PeterWright-不平衡的引号可以通过延迟扩展(“!line!”)轻松解决…,如果数据可能包含!
,还可以添加延迟扩展切换。如果您希望帮助可靠地解析此内容,请向我们显示myfile中的内容,并解释您希望在outfile中获得的内容。我假设存在
第2.myfile.txt行中的test d
之前是一个文本文件,包含以下行“s test1”、“test b”、“stest1”、“s test2”、“test d”、“stest2”,因此我想从每行中提取第一个和第三个字符串,如“s test1”和“stest1”
@echo off &SETLOCAL
for /f "tokens=* delims= " %%f in (file) do (
set "line=%%f"
call :processToken
)
goto :eof
:processToken
ECHO "%line%"
for /f "tokens=1* delims=/" %%a in ("%line%") do (
echo Got one token: "%%a"
set "line=%%b"
)
if not "%line%" == "" goto :processToken
goto :eof
@echo off
for /f "tokens=* delims= " %%f in (myfile) do (
set line=%%f
call :processToken
)
goto :eof
:processToken
for %%a in (%line%) do (
echo Got one token: %%a
)
goto :eof