I have simple files that look like:
Submit.log
Submitting job(s)..
2 job(s) submitted to cluster 528.
Submitting job(s)..
2 job(s) submitted to cluster 529.
I need then to output only the last number from the second line (the cluster), in this case the desired output would be:
528
529
I tried using the script below but I dont know why it is not working. I need to do it using batch, it is a restriction of our system.
@ECHO OFF
FOR /F "tokens=6" %%g IN (Submit.log) DO (
set Job=%%g
echo %Job:.=%
)
An elegant solution would definitively be a plus, but if someone could help me out with a simple solution I would appreciate.
Here is a one liner that works. The FINDSTR search can easily be adapted to make the search very precise.
@echo off
for /f "tokens=6 delims=. " %%N in ('findstr /c:"submitted to cluster" "submit.log"') do echo %%N
The paxdiablo solution can be simplified as well so that it does not need to modify the value therefor it doesn't need delayed expansion. There is no need to capture all the tokens, just the ones you want to use. The delimiter is set to look for dot and space (the default was space and tab). As paxdiablo said, more tokens can be tested to make the search more precise, but this is not as convenient as FINDSTR.
@echo off
for /f "tokens=5,6 delims=. " %%A in (submit.log) do if %%A==cluster echo %%B
The following script will do the job:
@setlocal enableextensions enabledelayedexpansion
@echo off
for /f "tokens=1-6" %%a IN (submit.log) do (
if %%c==submitted (
set job=%%f
echo !job:.=!
)
)
endlocal
And, since I'm basically the modest type, I'll stop short of contending that it's both elegant and simple :-)
It basically only looks at those lines where the third word is submitted
(you can also use additional rules if this proves problematic) and then modifies and outputs the sixth word.
Note the use of delayed expansion (using !
instead of %
) That's needed to ensure the variables aren't evaluated before the loop even begins.