0

I was given a script but need to modify it so it reads the 3 line Subversion update between the delimeters "---------".

So if we search the Text or Revision number or Jira ID, the script will return the complete SVN update of all 3 lines between/before and after the line that has "----------" as the separation.

  @echo off
  setlocal enableDelayedExpansion

 :: Define LF to contain a line feed character (0x0A)
 set ^"LF=^

 ^" The above empty line is critical - DO NOT REMOVE

:: Define CR to contain a carriage return character (0x0D)
for /f %%A in ('copy /Z "%~dpf0" nul') do set "CR=%%A"

:: Define the string to search for
set "s=%~1"

svn log https://subversion.company.com/svn/company-dev/|findstr /ri  
/c:"!s!" /c:"!lf!.*!s!" /c:"!lf!.*!cr!*!lf!.*!s!" /c
:"!lf!.*!cr!*!lf!.*!cr!*!lf!.*!s!" 

The output is usually like this:

-------------------------------------------------------------------------------------------------------------------------
r132536 | USERID | 2014-05-06 15:20:06 -0700 (Tue, 06 May 2014) | 2 lines

Interim checkin - use part rev from as worked item if exists
AERO ID: AERO-1107
------------------------------------------------------------------------------------------------------------------------- 

So if any text is found between the "-----" print that text between the lines "----"

Gary Seven
  • 109
  • 1
  • 1
  • 7

2 Answers2

0

This should do the trick. Obviously replace subversion.log with the name of your file. It outputs to a file called output.log.

@echo off
setLocal enableDelayedExpansion
set v=0
for /f "delims=" %%a in (subversion.log) do (
    set a=%%a
    if !v!==1 (if "!a:~0,4!"=="----" (set v=0) else echo %%a>>output.log) else (
    if !v!==0  if "!a:~0,4!"=="----" (set v=1))
)
unclemeat
  • 5,029
  • 5
  • 28
  • 52
  • You could add the redirect character to the end of the command (`>` for write or `>>` for append), or you could use it when calling the script. i.e. `script.bat >> output.log` – unclemeat May 08 '14 at 00:42
  • Thanks. I would have to copy the log first since no direct access to SVN server. I was wondering if there was a direct way from the svn log command output inline. – – Gary Seven May 08 '14 at 04:24
  • This does not even attempt to search for a string between the `-------------` section delimiters. – dbenham May 08 '14 at 17:09
  • @dbenham I misinterpreted the question. This will get text between `----` lines in a text file and output to another text file. – unclemeat May 08 '14 at 22:58
0

Here is a simple solution that relies on a hybrid batch/JScript utility called REPL.BAT that performs a regular expression search and replace on stdin and writes the result to stdout. REPL.BAT is pure script that will run on any modern Windows machine from XP onward. Full documentation is embedded within the script.

This solution avoids creation of any temporary file, and it should be fairly fast, especially if your machine has multiple processors or cores.

@echo off
(echo(&svn log https://sub.mycompany.com/svn/company-dev/)|repl \r?\n \r mx|repl (\r-*\r) $1\n x|findstr /ic:%1|repl \r \r\n x

The code above uses a series of piped commands. In the explanation below, \r represents a carriage return (0x0D), and \n represents a linefeed (0x0A)

1) Echo a blank line, followed by the output of SVN. The blank line at the beginning is there to make sure the opening delimiting line is parsed properly.

2) Replace all newlines (\r\n, or \n) with a single \r. The content now consists of one long continuous line, with \r representing where the newlines used to be.

3) Append \n to each section delimiter. Now there is one section per line.

4) Use FINDSTR to keep the section(s) that contain your search string, and throw the rest away.

5) Replace all \r that are not paired with \n with \r\n. The found sections are now back to their original form.


Update in response to comments

Ouch! You have a lot of data to parse. If you want to stick with native scripting capabilities, then I think you will require a custom VBScript, JScript, or PowerShell solution.

But if you are in a position to download and use a 3rd party utility, then I think pcregrep.exe can help. It has an -M option that supports multi-line regular expression searches.

svn log https://sub.mycompany.com/svn/company-dev/ | pcregrep -M "^--*\n([\s\S](?!^--*$))*%~1([\s|\S](?!^--*$))*"
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • I added the REPL.BAt file to my folder and ran the primary script with the SVN command. The script is taking a long time with no output.(17 minutes) I can see the cscript memory rising in Task Manager. Running Windows 7 pro SP1 8GIG ram 64 bit OS 2.80GHZ processor. I could turn on echo on REPL.BAT but the output may be to much. I will check again in the morning – Gary Seven May 08 '14 at 04:30
  • Whoa! How large is your SVN output? It works fine on my 4 core 64bit Win7 machine when I use a small test file instead of SVN output. But step 2) must load the entire SVN output into memory, which could be a problem if the output is very large. It could be improved some if temp files are used instead of pipes, but that kind of destroys the "elegance" of the solution. – dbenham May 08 '14 at 14:33
  • I got an error that line 225333 is too long. It is a blank line when I copy the log and no other lines around it appear long. The file when copied is 27,000kb. Got this error: C:\BAT>svnfindtext r132570 FINDSTR: Line 225333 is too long. ------------------------------------------------------------------------ r132570 | marcel | 2014-05-07 06:05:12 -0700 (Wed, 07 May 2014) | 3 lines '\r' is not recognized as an internal or external command, operable program or batch file. – Gary Seven May 08 '14 at 16:18
  • @user3609755 - OK, large content! See my updated answer. – dbenham May 08 '14 at 17:28
  • @user3609755 - Don't forget to accept an answer if it fully answers your question. – dbenham May 08 '14 at 19:22
  • Is there a way to parse the output and put the lines in excel column format? Lines output like this : #3 #1 #2 and truncate the space instead lines in the order of output – Gary Seven May 16 '14 at 04:25
  • @user3609755 - Certainly. Why don't you read up on regular expressions, as well as the JavaScript replace method, and give it a try. The HELP embedded in REPL.BAT includes links to good MicroSoft regex docs. – dbenham May 18 '14 at 21:46