Extract specific value from command line output using powershell

Here's the problem: I want to request subversion for the version number of the repository, and then create a new directory with the revision number in its name (for example, "Build763").

The svn info command displays several pairs of characters \ value separated by colons. for instance

Path: c#
URL: file:///%5Copdy-doo/Archive%20(H)/Development/CodeRepositories/datmedia/Development/c%23
Repository UUID: b1d03fca-1949-a747-a1a0-046c2429f46a
Revision: 58
Last Changed Rev: 58
Last Changed Date: 2011-01-12 11:36:12 +1000 (Wed, 12 Jan 2011)

Here is my current code that solves the problem. Is there a way to do this with less code? I think using pipelines you can do this with one or two lines. Of course, I should not use a temporary file.

$RepositoryRoot = "file:///%5Cdat-ftp/Archive%20(H)/Development/CodeRepositories/datmedia"
$BuildBase="/Development/c%23"
$RepositoryPath=$RepositoryRoot + $BuildBase

# Outputing the info into a file
svn info $RepositoryPath | Out-File -FilePath svn_info.txt

$regex = [regex] '^Revision: (\d{1,4})'

foreach($info_line in get-content "svn_info.txt")
    {
        $match = $regex.Match($info_line);
        if($match.Success)
        {
            $revisionNumber = $match.Groups[1];
            break;
        }
    }


"Revision number = " + $revisionNumber;
+3
source share
4 answers

Here is one way:

    $revisionNumber = svn info $RepositoryPath |
     select-string "^revision" |
      foreach {$_.line.split(":")[1].trim()}

    if ($revisionNumber){"Revision number = " + $revisionNumber

}
+3
source

mjolinor, , , :

$revisionNumber = (svn info $RepositoryPath | select-string '^Revision: (\d+)$').Matches[0].Groups[1].Value

! . ( , , )

+2
+2
source

If you can create and use a working copy of the URL (WC), you can

  • svnversion WC: displays the net number of WC revisions

    > svnversion trunk

    34

  • With SubWCRrev, convert the template file from SubWCRrev-keywords to bat file ( subwcrev WC tpl-file bat-file) with RevNo, replaced in the right place (inside mkdir parameters) and called bat to perform the operation
0
source

Source: https://habr.com/ru/post/1785329/


All Articles