Place Substring of Piped Output Into File

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #5603

    by soloProgrammer at 2013-02-27 08:53:03

    Hi,
    I'm trying to do something I think is simple, but can't seem to get it to work. I'd appreciate your help.

    I'm trying to simply create a C define based on the last changed SVN version number. I want to do this all on one line so I can use it within the IAR build tool as a pre-build command, but right now, I'd just like to get it to work.

    I've done this successfully:
    $a=svn info | find "Last Changed Rev: ";"-D BUILD_REVISION=" + $a.substring(18,5) | out-file buildrev.txt

    But when I tried to do this, it wouldn't work:
    powershell -command $a=svn info | find "Last Changed Rev: ";"-D BUILD_REVISION=" + $a.substring(18,5) | out-file buildrev.txt

    I've tried using select-string instead like this, but this doesn't work:
    svn info | select-string "Last Changed Rev: " | where-object {$_.substring(18, 5)}

    It seems here the output from select-string is not a string, but rather a number? It says there's no substring method of the object. When I use find, it appears to work, but then I can't seem to get that to run using powershell -command.

    I'd prefer this on one line so I can use it directly in the IAR tool as a pre-build command, but at this point, I'll take whatever works.

    Thanks for your help!
    Tony

    by DonJ at 2013-02-27 09:01:07

    The output of Select-String will depend a bit, but it's definitely not a number. It can be a result object – but it can also be a collection of strings, and in PowerShell v2 you can't call a method on a collection. Try piping Select-String's output to GM.

    I'm curious, since you got your first command to work, could you not put it in a .PS1 file and call it using PowerShell.exe? In other words, instead of trying to feed the command to -command (which likely won't work because of the complexity of the syntax), try putting in a file, and running the file? I mean, if you have a working command... let's work with that instead of reinventing?

    by mjolinor at 2013-02-27 11:19:22

    That looks like it could be easily solved using a simple get-content with a chained -match -replace. If you can post a sample of a complete line to write the regex from, I'll see what I can do........

    by soloProgrammer at 2013-02-27 11:53:10

    Thanks DonJ,
    I tried running it from a file, but now I'm banging into restriction policy problems. I tried to change them (per http://technet.microsoft.com/en-us/libr ... 76949.aspx), but it seems I can't change the registry. I'm checking with IT on that now. But while this may end up being possible, I really wanted this done as a single command line that can execute from another tool, if possible.

    Hi mjolinor:

    The output from svn info looks something like this:

    Path: .
    Working Copy Root Path: C:\foobar
    URL:
    Repository Root:

    Repository UUID:

    Revision: 33311
    Node Kind: directory
    Schedule: normal
    Last Changed Author:

    Last Changed Rev: 33294
    Last Changed Date: 2013-02-25 15:14:03 -0800 (Mon, 25 Feb 2013)

    When I used the first command shown above, the contents of file buildrev.txt are:
    -D BUILD_REVISION=33294

    Thanks,
    Tony

    by mjolinor at 2013-02-27 12:25:19

    Does this work for you?
    $regex = '^Last Changed Rev: (\d{5})\s*$'

    (svn info) -match $regex -replace $regex,'-D BUILD_REVISION=$1'

    by soloProgrammer at 2013-02-28 11:07:09

    Hi,

    Both the regex approach and using my command in a script work from the DOS Command line (thank you both for that). But, when I use the -file approach from IAR pre-build, it doesn't generate an error, but it doesn't produce a file either. When I use the -command approach, I get a file, but it is empty. IAR is not being very helpful, and there are no messages that I can see that help explain what's going wrong, so I'm kind of stuck. Any thoughts based on prior experience of invoking powershell from another application would be appreciated.

    In general though (as I'm new to Powershell) I'd like to understand why I can't directly use something that is piped through? That is, why can't I do this:
    svn info | select-string "Last Changed Rev: " | "-D BUILD_REV=" + $_.substring(18, 5)

    This seems natural to me and it seems like the select-string should send the string object on through the pipe and $_ should use it as it is the current object. Then I feel I should be able to pipe that to output-fle. But this doesn't work. I"m still not clear exactly what's coming through the pipe after select-string. If this is all described somewhere, just point me in the right direction.

    Thanks,
    Tony

    by mjolinor at 2013-02-28 11:50:42

    Select-String doesn't produce a string object. It produces a MatchInfo object, which is considerably more complex thant a simple string. The -match -replace does produce a string object.

    You can only pipeline input into a command that is designed to accept input from the pipeline, so this does not work:

    svn info | select-string "Last Changed Rev: " | "-D BUILD_REV=" + $_.substring(18, 5)

    "-D BUILD_REV=" + $_.substring(18, 5) is just a string concatenation expression, there is no command.

    Not tested, but I think this might work:

    svn info |
    select-string "Last Changed Rev: " |
    select-object -ExpandProperty line |
    foreach-object {"-D BUILD_REV=" + $_.substring(18, 5)}

You must be logged in to reply to this topic.