我有三个变量可以说是SOURCE_SUCCESS,JULIANDAYS,APPROVEREJECT
SOURCE_SUCCESS的值= a,b,c,d,e,f
JULIANDAYS的值= 1,2,3,4,5
APPROVEREJECT的值= A,R,A,A,R
现在我想要做的是使用批处理文件命令以如下所示的格式打印三个变量的值
a 1 A
b 2 R
等等
还有一点我想将其重定向到文件
答案 0 :(得分:1)
这种基于DOS的方法适用于您的特定要求:
@echo off
set SOURCE_SUCCESS=a,b,c,d,e,f
set JULIANDAYS=1,2,3,4,5
set APPROVEREJECT=A,R,A,A,R
:ROW1
for /f "tokens=1" %%j in ('echo %SOURCE_SUCCESS%') do set col1=%%j
for /f "tokens=1" %%j in ('echo %JULIANDAYS%') do set col2=%%j
for /f "tokens=1" %%j in ('echo %APPROVEREJECT%') do set col3=%%j
echo %col1% %col2% %col3% > myfile.txt
:ROW2
for /f "tokens=2" %%j in ('echo %SOURCE_SUCCESS%') do set col1=%%j
for /f "tokens=2" %%j in ('echo %JULIANDAYS%') do set col2=%%j
for /f "tokens=2" %%j in ('echo %APPROVEREJECT%') do set col3=%%j
echo %col1% %col2% %col3% >> myfile.txt
:ROW3
for /f "tokens=3" %%j in ('echo %SOURCE_SUCCESS%') do set col1=%%j
for /f "tokens=3" %%j in ('echo %JULIANDAYS%') do set col2=%%j
for /f "tokens=3" %%j in ('echo %APPROVEREJECT%') do set col3=%%j
echo %col1% %col2% %col3% >> myfile.txt
:ROW4
for /f "tokens=4" %%j in ('echo %SOURCE_SUCCESS%') do set col1=%%j
for /f "tokens=4" %%j in ('echo %JULIANDAYS%') do set col2=%%j
for /f "tokens=4" %%j in ('echo %APPROVEREJECT%') do set col3=%%j
echo %col1% %col2% %col3% >> myfile.txt
:ROW5
for /f "tokens=5" %%j in ('echo %SOURCE_SUCCESS%') do set col1=%%j
for /f "tokens=5" %%j in ('echo %JULIANDAYS%') do set col2=%%j
for /f "tokens=5" %%j in ('echo %APPROVEREJECT%') do set col3=%%j
echo %col1% %col2% %col3% >> myfile.txt
输出:
a 1 A
b 2 R
c 3 A
d 4 A
e 5 R
如果您拥有可变数量的令牌,我还尝试了以下更灵活的方法,但似乎DOS拒绝令牌说明符中的变量(无论是否使用延迟变量扩展)。也许别人知道为什么?:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set SOURCE_SUCCESS=a,b,c,d,e,f
set JULIANDAYS=1,2,3,4,5
set APPROVEREJECT=A,R,A,A,R
REM The following doesn't work because variable as 'tokens' are rejected
for /L %%i in (1,1,5) Do (
set fieldnum=%%i
for /f "tokens=!fieldnum!" %%j in ('echo %SOURCE_SUCCESS%') do echo %%j
for /f "tokens=!fieldnum!" %%k in ('echo %JULIANDAYS%') do echo %%k
for /f "tokens=!fieldnum!" %%l in ('echo %APPROVEREJECT%') do echo %%l
)
答案 1 :(得分:1)
下面的批处理文件适用于任意数量的令牌/字段:
@echo off
setlocal EnableDelayedExpansion
set SOURCE_SUCCESS=a,b,c,d,e,f
set JULIANDAYS=1,2,3,4,5
set APPROVEREJECT=A,R,A,A,R
set i=0
for %%a in (%SOURCE_SUCCESS%) do (
set /A i+=1
set result[!i!]=%%a
)
set /A n=i, i=0
for %%a in (%JULIANDAYS%) do (
set /A i+=1
for %%i in (!i!) do set result[%%i]=!result[%%i]! %%a
)
if %i% gtr %n% set n=%i%
set i=0
for %%a in (%APPROVEREJECT%) do (
set /A i+=1
for %%i in (!i!) do set result[%%i]=!result[%%i]! %%a
)
if %i% gtr %n% set n=%i%
for /L %%i in (1,1,%n%) do echo !result[%%i]! >> thefile.txt
结果如下:
a 1 A
b 2 R
c 3 A
d 4 A
e 5 R
f
编辑:添加新方法
下面的批处理文件较小,可以更轻松地处理任意数量的变量:
@echo off
setlocal EnableDelayedExpansion
set SOURCE_SUCCESS=a,b,c,d,e,f
set JULIANDAYS=1,2,3,4,5
set APPROVEREJECT=A,R,A,A,R
set n=0
for %%v in (SOURCE_SUCCESS JULIANDAYS APPROVEREJECT) do (
set i=0
for %%a in (!%%v!) do (
set /A i+=1
for %%i in (!i!) do set result[%%i]=!result[%%i]! %%a
)
if !i! gtr !n! set n=!i!
)
for /L %%i in (1,1,%n%) do echo !result[%%i]! >> thefile.txt
答案 2 :(得分:0)
$ paste <(echo $SOURCE_SUCCESS | tr ',' '\n') <(echo $JULIANDAYS | tr ',' '\n') <(echo $APPROVEREJECT | tr ',' '\n') >output.txt
$ cat output.txt
a 1 A
b 2 R
c 3 A
d 4 A
e 5 R
f
注意:如果将所有值逐行放入单独的文件中会更容易。