Hi there, i need a little help with parsing this text for example:
------------------------------------------------------------------------
r637 | johndoe | 2009-03-03 20:22:37 +0100 (tue, 03 mar 2009) | 1 line
------------------------------------------------------------------------
r636 | johndoe | 2009-03-03 20:21:25 +0100 (tue , 03 mar 2009) | 1 line
T_090303
------------------------------------------------------------------------
r635 | johndoe | 2009-03-03 20:15:33 +0100 (tue, 03 mar 2009) | 1 line
------------------------------------------------------------------------
r389 | johndoe | 2009-02-02 15:14:53 +0100 (mon, 02 feb 2009) | 1 line
T_090202
------------------------------------------------------------------------
I need to take revisson number (r636 for example) from the last tagged version (T_090303). So in this case i would like to store r636 in a variable because T_090303 means it is the last tagged version.
I've tried to do that using for /f but i don't know how to do it right.
Thanks in advance.
Help with parsing
Moderator: DosItHelp
-
- Posts: 18
- Joined: 16 Mar 2009 08:18
- Location: N/A
- Contact:
I THINK THIS SHOULD DO THE TRICK...
FIRST:
WE CREATE A SEPERATE
TEXT DOCUMENT TO HOUSE
THE ORIGINALL TEXT
COPY AND PASTE THE BELOW
TEXT AND SAVE AS FILE.TXT
------------------------------------------------------------------------
r637 | johndoe | 2009-03-03 20:22:37 +0100 (tue, 03 mar 2009) | 1 line
------------------------------------------------------------------------
r636 | johndoe | 2009-03-03 20:21:25 +0100 (tue , 03 mar 2009) | 1 line
T_090303
------------------------------------------------------------------------
r635 | johndoe | 2009-03-03 20:15:33 +0100 (tue, 03 mar 2009) | 1 line
------------------------------------------------------------------------
r389 | johndoe | 2009-02-02 15:14:53 +0100 (mon, 02 feb 2009) | 1 line
T_090202
------------------------------------------------------------------------
SECOND:
WE CREATE THE SCRIPT
THAT WILL READ THE FILE
COPY AND PASTE THE CODE
BELOW SAVE AS WHATEVER
.BAT .CMD AND PUT THE BATCH
FILE AND THE TEXT FILE IN
THE SAME DIRECTORY
NOW OPTIONALLY WE COULD ADD %1 %2 ETC . . .
TO THE TOP OF THIS SCRIPT ALLOWING US TO
INVOKE IT FROM THE COMMAND LINE LIKE SO . . .
REVISED:
THE ABOVE SCRIPT WOULD ALLOW US TO INVOKE IT
FROM THE COMMAND LINE BY TYPING
[TEST.BAT CALL -BG]
THIS WOULD, IF OUR SCRIPT WAS CALLED TEST.BAT
FIRST LAUNCH TEST.BAT THAN WOULD
PASS THE "CALL" PARAMETER TO %1 AND FINALLY THE
APPENDED SWITCH "-BG", TO %2, WHICH WOULD AFTER
THE LOCAL ENVIRONMENT LOADS, TAKE US STRAIGHT TO
THE :BEGIN LABEL IF MORE CODE WAS AVAILABLE
.... %3 %4 %5 %6 %7 %8 %9
COULD ALL HAVE ARGUMENTS PASSED TO THEM
TO BE EXECUTED ONCE THE SCRIPT WAS LAUNCHED.
THERE IS AS WELL A %0 THIS IS LEFT TO REPRESENT
THE ORIGINAL FILE THAT EXECUTED THE APPLICATION
IF THE SHIFT COMMAND IS PRESENT THE PARAMETER
VARAIABLES CHANGE AS FOLLOWS....
.... %1 BECOMES %0 ; %2 BECOMES %1 AND SO FORTH.
PRETTY BASIC KNOWLEDGE I KNOW BUT IT NEVER HURTS
TO HAVE A REFRESHER... HOPE THIS HELPS.
WE CREATE A SEPERATE
TEXT DOCUMENT TO HOUSE
THE ORIGINALL TEXT
COPY AND PASTE THE BELOW
TEXT AND SAVE AS FILE.TXT
------------------------------------------------------------------------
r637 | johndoe | 2009-03-03 20:22:37 +0100 (tue, 03 mar 2009) | 1 line
------------------------------------------------------------------------
r636 | johndoe | 2009-03-03 20:21:25 +0100 (tue , 03 mar 2009) | 1 line
T_090303
------------------------------------------------------------------------
r635 | johndoe | 2009-03-03 20:15:33 +0100 (tue, 03 mar 2009) | 1 line
------------------------------------------------------------------------
r389 | johndoe | 2009-02-02 15:14:53 +0100 (mon, 02 feb 2009) | 1 line
T_090202
------------------------------------------------------------------------
SECOND:
WE CREATE THE SCRIPT
THAT WILL READ THE FILE
COPY AND PASTE THE CODE
BELOW SAVE AS WHATEVER
.BAT .CMD AND PUT THE BATCH
FILE AND THE TEXT FILE IN
THE SAME DIRECTORY
Code: Select all
@ECHO OFF
:BEGIN
FOR /F "EOL= TOKENS=1 DELIMS= " %%X IN (
'FINDSTR /B /L "r636" FILE.TXT'
) DO (
SET r636=%%X
)
FOR /F "EOL= TOKENS=1 DELIMS= " %%Y IN (
'FINDSTR /B /L "T_090303" FILE.TXT'
) DO (
SET T_090303=%%Y
)
IF "%T_090303%" == "T_090303" (
SET %r636%=NEW_VAR
) ELSE (
GOTO :SKIP
)
ECHO %T_090303% WAS FOUND
ECHO THE NEW VAR FOR r636 IS %r636&
GOTO :EOF
:SKIP
ECHO STRING T_090303 NOT FOUND
ECHO VAR FOR r636 WILL NOT BE SET
PAUSE
GOTO :EOF
NOW OPTIONALLY WE COULD ADD %1 %2 ETC . . .
TO THE TOP OF THIS SCRIPT ALLOWING US TO
INVOKE IT FROM THE COMMAND LINE LIKE SO . . .
REVISED:
Code: Select all
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
%1 :%2 %3
:BEGIN
:-BG
FOR /F "EOL= TOKENS=1 DELIMS= " %%X IN (
'FINDSTR /B /L "r636" FILE.TXT'
) DO (
SET r636=%%X
)
FOR /F "EOL= TOKENS=1 DELIMS= " %%Y IN (
'FINDSTR /B /L "T_090303" FILE.TXT'
) DO (
SET T_090303=%%Y
)
IF "!T_090303!" == "T_090303" (
SET !r636!=NEW_VAR
) ELSE (
GOTO :SKIP
)
ECHO !T_090303! WAS FOUND
ECHO THE NEW VAR FOR r636 IS !r636!
GOTO :EOF
:SKIP
:-SK
ECHO STRING T_090303 NOT FOUND
ECHO VAR FOR r636 WILL NOT BE SET
PAUSE
GOTO :EOF
ENDLOCAL
THE ABOVE SCRIPT WOULD ALLOW US TO INVOKE IT
FROM THE COMMAND LINE BY TYPING
[TEST.BAT CALL -BG]
THIS WOULD, IF OUR SCRIPT WAS CALLED TEST.BAT
FIRST LAUNCH TEST.BAT THAN WOULD
PASS THE "CALL" PARAMETER TO %1 AND FINALLY THE
APPENDED SWITCH "-BG", TO %2, WHICH WOULD AFTER
THE LOCAL ENVIRONMENT LOADS, TAKE US STRAIGHT TO
THE :BEGIN LABEL IF MORE CODE WAS AVAILABLE
.... %3 %4 %5 %6 %7 %8 %9
COULD ALL HAVE ARGUMENTS PASSED TO THEM
TO BE EXECUTED ONCE THE SCRIPT WAS LAUNCHED.
THERE IS AS WELL A %0 THIS IS LEFT TO REPRESENT
THE ORIGINAL FILE THAT EXECUTED THE APPLICATION
IF THE SHIFT COMMAND IS PRESENT THE PARAMETER
VARAIABLES CHANGE AS FOLLOWS....
.... %1 BECOMES %0 ; %2 BECOMES %1 AND SO FORTH.
PRETTY BASIC KNOWLEDGE I KNOW BUT IT NEVER HURTS
TO HAVE A REFRESHER... HOPE THIS HELPS.
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
So basically you want a script that will find T_XXXXXX and then give you the rXXxxxx from the 2nd line after?
Code: Select all
@echo off
setlocal enabledelayedexpansion
set skip=0
for /f %%a in (file.txt) do (
echo %%a|findstr /B "T_" >nul 2>nul
if not errorlevel 1 (
set skip=1
) else (
if !skip!==1 (
set skip=2
) else (
if !skip!==2 set revision=%%a && goto :finish
)
)
)
:finish
if not defined revision (
echo We didn't find a tagged revision.
) else (
echo Tagged revision: %revision%
)
-
- Posts: 18
- Joined: 16 Mar 2009 08:18
- Location: N/A
- Contact:
HMMM.
At risk of blowing my own horn, umm I did the same thing and you should
read the fine print more, where is r636, and when does it get set you
left that out AVERY even if the T_ var is found then what happenes with r636
You have alot of extra nonsense that just finds T_VAR and says we found it
but the representation between the 2 strings
is linked, and r636 may be present even if T_var is not so r636 would set
and T_VAR would be the last tagged version.There still needs to be
refrence to the r636 var as in what to do with it even if you find T_VAR
or not. Just a common sense observation.
Cute Code tho...
Read twice next time before posting please
read the fine print more, where is r636, and when does it get set you
left that out AVERY even if the T_ var is found then what happenes with r636
You have alot of extra nonsense that just finds T_VAR and says we found it
but the representation between the 2 strings
is linked, and r636 may be present even if T_var is not so r636 would set
and T_VAR would be the last tagged version.There still needs to be
refrence to the r636 var as in what to do with it even if you find T_VAR
or not. Just a common sense observation.
Cute Code tho...
Read twice next time before posting please
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
OP wants . . ?
Hmmm . . . Maybe you're right. What do you think the OP is asking for?
To me it sounded like there's a log file of some type which contains information on various revisions (perhaps of code?). Some of these revisions are also tagged (perhaps to represent stable code or major changes?).
The OP seems to want the revision number of the most recent tagged revision. T_090303 and r636 seem to be examples of a tag and a revision number (one could hypothesize that the 090303 portion of the tag in this example reprents year, month, day).
My code will read the file one line at a time and find the first line that begins with T_ and at this point I assume that we have found the latest tagged version. From here, my code effectively skips the next line and then sets the variable "revision" equal to whatever the first token is on that line -- in this case r635.
As you can see, I did in fact mess up. Instead, I believe it should have been the revision number rXXXxxx that comes BEFORE the first T_XXXXXX tag. To that end I would present the following (much easier) code:
My apologies for misreading the OP.
Further apologies if I have still misunderstood the OP.
To me it sounded like there's a log file of some type which contains information on various revisions (perhaps of code?). Some of these revisions are also tagged (perhaps to represent stable code or major changes?).
The OP seems to want the revision number of the most recent tagged revision. T_090303 and r636 seem to be examples of a tag and a revision number (one could hypothesize that the 090303 portion of the tag in this example reprents year, month, day).
My code will read the file one line at a time and find the first line that begins with T_ and at this point I assume that we have found the latest tagged version. From here, my code effectively skips the next line and then sets the variable "revision" equal to whatever the first token is on that line -- in this case r635.
As you can see, I did in fact mess up. Instead, I believe it should have been the revision number rXXXxxx that comes BEFORE the first T_XXXXXX tag. To that end I would present the following (much easier) code:
Code: Select all
@echo off
setlocal
for /f "tokens=1*" %%a in (file.txt) do (
echo %%a|findstr /B "T_" >nul 2>nul
if not errorlevel 1 set tag=OK && goto :finish
set revision=%%a
set revinfo=%%b
)
:finish
if not defined tag (
echo We didn't find a tag
pause
goto :eof
)
if not defined revision (
echo We didn't find a revision to go with the tag.
pause
goto :eof
)
echo Tagged revision: %revision%
echo Tagged revision info: "%revinfo%"
My apologies for misreading the OP.
Further apologies if I have still misunderstood the OP.