2012-12-05 51 views
0

想象一下.txt文件:查找表中的字符串並返回一個值

Example.txt 

Row Column1 Column2 Column3 <br> 
1  aaa   bbb  ccc<br> 
2  ddd   eee  fff 

我怎樣才能在使用命令行的表中的值?

Ex。 :(Row = 2,Column2)返回值「eee」。例子:
Ex。 :(行= 1,列3)返回值「ccc」。我想嘗試findstr但我不知道如何實現它。

+0

物理是行號信息的文件作爲第1列? – dbenham

回答

0

假設該行信息實際上是在文件中,並假設沒有值包含空格:

@echo off 
call :getValue 2 Column2 "Example.txt" 
call :getValue 1 Column3 "Example.txt" 
call :getValue 0 Column1 "Example.txt" 
call :getValue 1 DoesNotExist "Example.txt" 
exit /b 

:getValue 
setlocal 
<%3 set /p "header=" 
set token=0 
for %%C in (%header%) do (
    set /a token+=1 
    if %%C==%2 goto columnFound 
) 
echo Column %2 not found in %3 
exit /b 

:columnFound 
for /f "tokens=%token%" %%A in ('findstr /blc:"%1 " %3') do (
    echo %3 (Row=%1, %2^) = %%A 
    exit /b 
) 
echo Row %1 not found in %3 
exit /b 

結果

"Example.txt" (Row=2, Column2) = eee 
"Example.txt" (Row=1, Column3) = ccc 
Row 0 not found in "Example.txt" 
Column DoesNotExist not found in "Example.txt" 
相關問題