Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8890119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:25:37+00:00 2026-06-14T22:25:37+00:00

I need to parse a text (output from a svn command) in order to

  • 0

I need to parse a text (output from a svn command) in order to retrieve a number (svn revision).
This is my code. Note that I need to retrieve all the output stream as a text to do other operations.

def proc = cmdLine.execute()                 // Call *execute* on the strin
proc.waitFor()                               // Wait for the command to finish
def output = proc.in.text

//other stuff happening here

output.eachLine {
    line ->
    def revisionPrefix = "Last Changed Rev: "
    if (line.startsWith(revisionPrefix)) res = new Integer(line.substring(revisionPrefix.length()).trim())
}

This code is working fine, but since I’m still a novice in Groovy, I’m wondering if there were a better idiomatic way to avoid the ugly if…

Example of svn output (but of course the problem is more general)

Path: .
Working Copy Root Path: /svn
URL: svn+ssh://svn.company.com/opt/svnserve/repos/project/trunk
Repository Root: svn+ssh://svn.company.com/opt/svnserve/repos
Repository UUID: 516c549e-805d-4d3d-bafa-98aea39579ae
Revision: 25447
Node Kind: directory
Schedule: normal
Last Changed Author: ubi
Last Changed Rev: 25362
Last Changed Date: 2012-11-22 10:27:00 +0000 (Thu, 22 Nov 2012)

I’ve got inspiration from the answer below and I solved using find(). My solution is:

def revisionPrefix = "Last Changed Rev: "
def line = output.readLines().find { line -> line.startsWith(revisionPrefix) }
def res = new Integer(line?.substring(revisionPrefix.length())?.trim()?:"0")

3 lines, no if, very clean

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:25:38+00:00Added an answer on June 14, 2026 at 10:25 pm

    One possible alternative is:

    def output = cmdLine.execute().text
    Integer res = output.readLines().findResult { line ->
      (line =~ /^Last Changed Rev: (\d+)$/).with { m ->
        if( m.matches() ) {
          m[ 0 ][ 1 ] as Integer
        }
      }
    }
    

    Not sure it’s better or not. I’m sure others will have different alternatives

    Edit:

    Also, beware of using proc.text. if your proc outputs a lot of stuff, then you could end up blocking when the inputstream gets full…

    Here is a heavily commented alternative, using consumeProcessOutput:

    // Run the command
    String output = cmdLine.execute().with { proc ->
    
      // Then, with a StringWriter
      new StringWriter().with { sw ->
    
        // Consume the output of the process
        proc.consumeProcessOutput( sw, System.err )
    
        // Make sure we worked
        assert proc.waitFor() == 0
    
        // Return the output (goes into `output` var)
        sw.toString()
      }
    }
    
    // Extract the version from by looking through all the lines
    Integer version = output.readLines().findResult { line ->
    
      // Pass the line through a regular expression
      (line =~ /Last Changed Rev: (\d+)/).with { m ->
    
        // And if it matches
        if( m.matches() ) {
    
          // Return the \d+ part as an Integer
          m[ 0 ][ 1 ] as Integer
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to parse a text file that contains unstructured text. I need to
I need a regular expression to parse a body of text. Basically assume this
Need to parse a file for lines of data that start with this pattern
New to Perl. I need to parse a report that look like this: 2012-05-29@emaillocalpart@emaildomain@customerid@errormessage@messageid
I need to parse some text in a UITextField and turn it into a
I need a regular expression to parse a text The directory is /home/foo/bar/hello.txt. I
I need the Perl regex to parse plain text input and convert all links
I have a verbatim environment containing computer output as text. This text is sematically
I have a text file that lists the names of a large number of
I need to download and parse webpage with lxml and build UTF-8 xml output.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.