文件是unix格式,内容如下:
export STR1="1"
export STR2="30"
export STR3="0024"
需要将STR1/2/3的值提取到新变量中,然后组合
类似1-30-0024
不熟悉BAT,请教大侠如何操作
@echo off
setlocal
set STR1=
set STR2=
set STR3=
for /f "usebackq tokens=2,3 delims== " %%a in ("%~1") do call :add %%a %%b
echo %STR1%-%STR2%-%STR3%
endlocal
goto end
:add
if "%1"=="STR1" set STR1=%~2
if "%1"=="STR2" set STR2=%~2
if "%1"=="STR3" set STR3=%~2
:end
----------------------
把横线上面代码保存为str.bat
把你的unix文件转换成Windows换行格式,假设文件名是a.txt
在命令行执行str.bat a.txt
输出:1-30-0024
setlocal
set STR1=
set STR2=
set STR3=
for /f "usebackq tokens=2,3 delims== " %%a in ("%~1") do call :add %%a %%b
echo %STR1%-%STR2%-%STR3%
endlocal
goto end
:add
if "%1"=="STR1" set STR1=%~2
if "%1"=="STR2" set STR2=%~2
if "%1"=="STR3" set STR3=%~2
:end
----------------------
把横线上面代码保存为str.bat
把你的unix文件转换成Windows换行格式,假设文件名是a.txt
在命令行执行str.bat a.txt
输出:1-30-0024
温馨提示:答案为网友推荐,仅供参考
第1个回答 2013-09-24
@echo off
set num=0
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%x in ('type ss') do (
set "str=%%x"
set/a num+=1
rem whether string have key words "STR%%i" or not, if so, executes next procedure
call,set str=%%str:STR!num!=%% && for /f "tokens=2 delims=^=" %%a in ('echo=%%x') do (
set string=!string!-%%a
)
rem strip double quotes in string
set string=!string:^"=!
)
rem get the string
echo=!string:~1!
endlocal
pause>nul
set num=0
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%x in ('type ss') do (
set "str=%%x"
set/a num+=1
rem whether string have key words "STR%%i" or not, if so, executes next procedure
call,set str=%%str:STR!num!=%% && for /f "tokens=2 delims=^=" %%a in ('echo=%%x') do (
set string=!string!-%%a
)
rem strip double quotes in string
set string=!string:^"=!
)
rem get the string
echo=!string:~1!
endlocal
pause>nul
第2个回答 2013-09-24
@echo off
for /f "delims== tokens=2" %%i in (a.txt) do (
echo %%i
)
pause>nul追问
for /f "delims== tokens=2" %%i in (a.txt) do (
echo %%i
)
pause>nul追问
这个操作仅仅是提取的字符串,现在还有""
另外,还需要将找到的字符串连接起来。