2016-08-09 1 views
0

から最後の列を取るために、私はラインを含むテキストファイルを持っています:バッチスクリプト - どのようにテキスト行

08-09 15:39:38.236 D/MVSDKTutorialBasicOpenCloseFileLoop(12054): availableMemory = 636 

を私は636の値を取得する必要があり、これまで私はこれを使用していた耕し:

for /f "tokens=6-8" %%i in (D:\Roey\Jen2\OpenCloseFileLoop\Results\Logcat\firstline.txt) do set revision=%%i 

問題は、somtimes行に余分なスペース(2054)があります - それは動作しません(私は=)、常に最後の列を取得する方法はありますか?

答えて

3

最後の要素の行に

for /F "delims=" %%a in (file.txt) do for %%b in (%%a) do set "revision=%%b" 
+0

それは素晴らしい、ありがとう!シンプルな1行のソリューション... –

2

賢くdelimsを選択します。

for /f "tokens=2 delims==" %%a in (file.txt) do set revision=%%a 
REM remove any spaces: 
set revision=%revision: =% 
+0

私の間違いは、それも素晴らしいことです...ありがとう! –

0

を事前に区切られた項目の番号がわからない場合は、テキストファイルは、任意のグローバルワイルドカード文字が含まれていないと仮定、次のコードスニペットを使用することができます*そして?

@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

rem /* Define constants here: */ 
set "FILE=D:\Roey\Jen2\OpenCloseFileLoop\Results\Logcat\firstline.txt" 
set "DELIM= " & rem // (define a single character here only) 

rem // Walk through the text file line by line: 
for /F usebackq^ delims^=^ eol^= %%L in ("%FILE%") do (
    set "LINE=%%L" 
    rem // Toggle delayed expansion to avoid loss of `!`: 
    setlocal EnableDelayedExpansion 
    rem // Double each `"` intermittently: 
    set "LINE=!LINE:"=""!^" & rem " 
    rem /* Enclose the entire line string within `""` and replace 
    rem each delimiter by `"` + SPACE + `"`; this results in a 
    rem SPACE-delimited list with each item enclosed within `""`: */ 
    set "LINE="!LINE:%DELIM%=" "!"" 
    rem /* Iterate through the built list using a standard `for` loop; 
    rem this works only in case no `*` or `?` occur in the string, 
    rem because `for` would consider them as global wildcards and 
    rem access the file system and through file-not-found errors: */ 
    for %%I in (!LINE!) do (
     endlocal 
     rem /* Save item in variable (overwrite previous one, so 
     rem last item is retrieved), and remove surrounding `""`: */ 
     if not "%%~I"=="" set "LAST=%%~I" 
     setlocal EnableDelayedExpansion 
    ) 
    rem // Return last item per line, revert doubling `"`: 
    if defined LAST echo(!LAST:""=^"! 
    endlocal 
) 

rem // Return last item of last line, revert doubling `"`: 
setlocal EnableDelayedExpansion 
if defined LAST echo(!LAST:""=^"! 
endlocal 

endlocal 
exit /B 
+0

私はあなたのコードを疲れましたが、それは単純なケースで動作しますが、他の人は "="再び受け取りました.... –

+0

私のスクリプトが失敗する入力データを教えてください... – aschipfl

+0

this one: "08-09 15 :39:38.236 D/MVSDKTutorialBasicOpenCloseFileLoop(12054):availableMemory = 636 "、スペースをランダムに追加してテストしてみてください。 –

関連する問題