Results 1 to 9 of 9
  1. #1
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts

    Need help understanding the pipe

    Hey Y'all,

    Ok, I just can't wrap my mind around why this works:
    Code:
     $Headers = "A","B","C","D","E","F","G","H","I","J","K" #1 letter per column!
    
      #--- Load File as CSV and add Headers to allow Sorting ---
      $x = import-csv -Path $FSpec.FullName  -Delimiter '|' -Header $Headers
      $x | ForEach-Object {$_.I = $_.I.padleft(10,'0')} 
      $x | Select-Object A,B,C,D,I,J,E,F,G,H,K | `
      Export-csv -Path $FSpec.FullName -Delimiter '|' -Force -NoTypeInformation
    and this does NOT!:
    Code:
     $Headers = "A","B","C","D","E","F","G","H","I","J","K" #1 letter per column!
    
      #--- Load File as CSV and add Headers to allow Sorting ---
      $x = import-csv -Path $FSpec.FullName  -Delimiter '|' -Header $Headers
      $x | ForEach-Object {$_.I = $_.I.padleft(10,'0')} | `
           Select-Object A,B,C,D,I,J,E,F,G,H,K | `
      Export-csv -Path $FSpec.FullName -Delimiter '|' -Force -NoTypeInformation
    It would seem that you should be able to just pass the information down the pipe from the ForEach-Object statement but when I try Export-csv just creates a 0 byte file! But if I terminate the pipe (per code example 1) and just start it over with $x the code work perfectly...I'm pulling what hair I have left out over this is just doesn't make any sense.
    Last edited by RetiredGeek; 2014-11-10 at 09:47.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  2. #2
    Super Moderator
    Join Date
    Aug 2012
    Location
    Durham UK
    Posts
    6,643
    Thanks
    147
    Thanked 883 Times in 844 Posts
    Although not into writing anything other than in BASIC a while back, from what I remember when reading about the Pipe - doesn't the command have to be on the same line after the Pipe for the redirect and not following a Return which would end the last command and the pipe.
    Last edited by Sudo15; 2014-11-10 at 09:57.

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Sudo,

    That would be the case if the line didn't end with the PS line continuation character { ` } in the second code snipit.
    Thanks for trying.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  4. #4
    Super Moderator
    Join Date
    Aug 2012
    Location
    Durham UK
    Posts
    6,643
    Thanks
    147
    Thanked 883 Times in 844 Posts
    Quote Originally Posted by RetiredGeek View Post
    Sudo,

    That would be the case if the line didn't end with the PS line continuation character { ` } in the second code snipit.
    Thanks for trying.
    Ah well

    Going back to BASIC - would a semi colon between the continuation and pipe or after the pipe not do it ?
    Last edited by Sudo15; 2014-11-10 at 10:39.

  5. #5
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Sudo,

    The semi-colon is a statement separator, e.g. separate statements on a single line. However, when using the pipe they are not single statements and it would cause an error to place a semi-colon following the pipe symbol.
    Code:
    PS> dir "G:\BEKDocs\*.csv" | ; `
    At line:1 char:25
    + dir "G:\BEKDocs\*.csv" | ; `
    +                         ~
    An empty pipe element is not allowed.
    At line:1 char:30
    + dir "G:\BEKDocs\*.csv" | ; `
    +                              ~
    Incomplete string token.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : EmptyPipeElement
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  6. #6
    Super Moderator
    Join Date
    Aug 2012
    Location
    Durham UK
    Posts
    6,643
    Thanks
    147
    Thanked 883 Times in 844 Posts
    I'll let you get on with it then

  7. #7
    Lounger
    Join Date
    Dec 2009
    Location
    Gillingham, Dorset, UK
    Posts
    30
    Thanks
    0
    Thanked 13 Times in 11 Posts
    RG,

    I believe that the reason is because, the 'ForEach-Object' is only updating the '$_.I' variable. It is not sending anything through the pipe.
    One way of doing it is to change your code as follows:

    Code:
    From:
    
      $x | ForEach-Object {$_.I = $_.I.padleft(10,'0')} | `
           Select-Object A,B,C,D,I,J,E,F,G,H,K | `
    
    To:
    $x | Select-Object A,B,C,D, @{label='I';expression={$_.I.PadLeft(10,'0')}},J,E,F,G,H,K |
    HTH

  8. The Following User Says Thank You to Cliff.H For This Useful Post:

    RetiredGeek (2014-11-11)

  9. #8
    Super Moderator BATcher's Avatar
    Join Date
    Feb 2008
    Location
    A cultural area in SW England
    Posts
    3,421
    Thanks
    33
    Thanked 195 Times in 175 Posts
    Usually I find a BATch file so much easier!
    BATcher

    Time prevents everything happening all at once...

  10. #9
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Quote Originally Posted by Cliff.H View Post
    RG,

    I believe that the reason is because, the 'ForEach-Object' is only updating the '$_.I' variable. It is not sending anything through the pipe.
    One way of doing it is to change your code as follows:

    Code:
    From:
    
      $x | ForEach-Object {$_.I = $_.I.padleft(10,'0')} | `
           Select-Object A,B,C,D,I,J,E,F,G,H,K | `
    
    To:
    $x | Select-Object A,B,C,D, @{label='I';expression={$_.I.PadLeft(10,'0')}},J,E,F,G,H,K |
    HTH
    Cliff,

    Thanks! that works great!
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •