我有三個變量讓說SOURCE_SUCCESS,JULIANDAYS,APPROVEREJECT在批處理文件重定向環境變量
SOURCE_SUCCESS = A,B,C,d,E的值,F
值JULIANDAYS的= 1,2,3,4,5
價值APPROVEREJECT = A,R,A,A的,R
現在我想在這裏做的是打印格式的三個變量的值如下所示使用批處理文件命令
1甲
B 2 - [R
等
一點我想此重定向到文件
我有三個變量讓說SOURCE_SUCCESS,JULIANDAYS,APPROVEREJECT在批處理文件重定向環境變量
SOURCE_SUCCESS = A,B,C,d,E的值,F
值JULIANDAYS的= 1,2,3,4,5
價值APPROVEREJECT = A,R,A,A的,R
現在我想在這裏做的是打印格式的三個變量的值如下所示使用批處理文件命令
1甲
B 2 - [R
等
一點我想此重定向到文件
$ 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
注:如果你把所有的值一行一行到分隔的文件,它會更容易些。
這種基於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
我也試過以下是在情況更加靈活,你曾經有一個可變數目的令牌,但它看起來拒絕令牌說明符中的變量(不管是否使用延遲變量擴展)。也許別人知道這是爲什麼?:
@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
)
以下工作批處理文件與任意數量的令牌/字段:
@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
想這在Windows批處理 – user101743 2012-03-02 15:40:07
您可以安裝'GNU-utils'窗戶。 – kev 2012-03-02 15:42:31
在相關說明中,它讓我感到困惑,Windows有一個'clip'程序,但不是''paste'「或'clip/p'。我發現使用getclip/putclip更改了行尾。 – ixe013 2012-03-02 16:38:28