none
for-each not displaying correct output

    Question

  • Hi,

    I am running into an issue where I need to read each and every row in a csv file and do a write-host. I need to read two columns from the csv file which I have defined as a variable.

    $TRAN = Import-Csv 'D:\TRAN.csv'
    $Results = @()
    foreach ($P in $TRAN)
    {
    $PL = $P.PlazaID; #Plaza ID
    $LN = $P.LaneNumber; #Plaza Lane

    write-host TRAN $PL $LN
    }

    When I run it, I get the following output. I am expecting to see TRAN PlazaID LaneNumber, However the $PL and $ LN values are not displayed.

    PS D:\.\TRAN.ps1
    TRAN
    TRAN
    TRAN
    TRAN

    I appreciate your help and time on this.

    Wednesday, July 17, 2013 9:09 PM

Answers

  • That is not a CSV file.

    See: http://en.wikipedia.org/wiki/Comma-separated_values

    Please try to use your search engine to find simple definitions of technical terms.  It will save you a lot of wasted time.


    ¯\_(ツ)_/¯

    Thursday, July 18, 2013 2:57 AM
  • If what you showd us is a correct image of your data file, it is not a csv (comma-seperated-values) file at all, as pointed out by jrv. That might be the cause of your problems.

    So, you have two options: First, you can modify your file into a valid csv file, or, second, you can replace 'Import-Csv' by something that reads out each line, splits them into single strings and assigns them to those properties of yours.

    To modify your file, you could do sth. similar to that snippet below:

    cd <yourFileDirPath>
    $objArr = @()
    type .\Tran.txt | % {if (-not [String]::IsNullOrEmpty($_)) {
        $arr = $_ -split "\s+" 
        $obj = New-Object PSObject 
        $obj | Add-Member -MemberType NoteProperty -Name "PlazaID" -Value $arr[0]
        $obj | Add-Member -MemberType NoteProperty -Name "LaneNumber" -Value $arr[1]
        $obj | Add-Member -MemberType NoteProperty -Name "LastUfmID" -Value $arr[2]
        $objArr += $obj
        }}
    $objArr | Export-Csv Tran.csv -NoTypeInformation

    wizend


    Thursday, July 18, 2013 8:44 AM
  • That looks like you're opening the csv file through Excel or something that formats the file.


    Can you open it with notepad and copy the exact format of the file into a code block here? It that is an exact copy of the file contents then jrv is right, it's not a CSV file and you'll need to take a different approach. If it is what you get in excel then it's a rendering of a csv file and it may still be possible to use either jsv or jaap's approaches.

    Thursday, July 18, 2013 9:03 AM

All replies

  • What does TRAN.csv look like?

    Bill

    Wednesday, July 17, 2013 9:20 PM
    Moderator
  • Does your csv file have a first line with the appropriate headers? If not, the properties 'PlazaID' and 'LaneNumber' won't be found.

    wizend

    Wednesday, July 17, 2013 9:26 PM
  • This is how to do that:

    $TRAN = Import-Csv D:\TRAN.csv
    foreach($P in $TRAN){
       Write-Host ('{0},{1}' -f $P.PlazaID, $P.LaneNumber)
    }

    That's it.


    ¯\_(ツ)_/¯

    Wednesday, July 17, 2013 9:34 PM
  • Hi All,

    I tried and it did not work.

    Here is how the Tran File Looks like

    PlazaID LaneNumber   Last UFM ID Last Txn Time
    1700 8
    1800 4
    1800 13
    3100 7
    3100 12
    3120 3
    3120 7

    I am interested only on the first and seconds columns PlazaID and LaneNumber. The next two columns are always blank.

    When I run this, I get the following output

     Write-Host ('{0},{1}' -f $U.PlazaID, $U.LaneNumber)

    PS D:\> .\LastUFMTransactions.ps1
    ,
    ,
    ,
    ,
    ,
    ,
    ,
    ,
    ,
    ,
    ,
    ,
    ,
    ,
    ,
    ,

    Thursday, July 18, 2013 2:49 AM
  • That is not a CSV file.

    See: http://en.wikipedia.org/wiki/Comma-separated_values

    Please try to use your search engine to find simple definitions of technical terms.  It will save you a lot of wasted time.


    ¯\_(ツ)_/¯

    Thursday, July 18, 2013 2:57 AM
  • I think using select object would be an easier solution here, try the following line of code:

    Import-Csv 'D:\TRAN.csv' | Select-Object -Property PlazaID,LaneNumber


    Jaap Brasser
    http://www.jaapbrasser.com

    Thursday, July 18, 2013 6:07 AM
    Moderator
  • If what you showd us is a correct image of your data file, it is not a csv (comma-seperated-values) file at all, as pointed out by jrv. That might be the cause of your problems.

    So, you have two options: First, you can modify your file into a valid csv file, or, second, you can replace 'Import-Csv' by something that reads out each line, splits them into single strings and assigns them to those properties of yours.

    To modify your file, you could do sth. similar to that snippet below:

    cd <yourFileDirPath>
    $objArr = @()
    type .\Tran.txt | % {if (-not [String]::IsNullOrEmpty($_)) {
        $arr = $_ -split "\s+" 
        $obj = New-Object PSObject 
        $obj | Add-Member -MemberType NoteProperty -Name "PlazaID" -Value $arr[0]
        $obj | Add-Member -MemberType NoteProperty -Name "LaneNumber" -Value $arr[1]
        $obj | Add-Member -MemberType NoteProperty -Name "LastUfmID" -Value $arr[2]
        $objArr += $obj
        }}
    $objArr | Export-Csv Tran.csv -NoTypeInformation

    wizend


    Thursday, July 18, 2013 8:44 AM
  • That looks like you're opening the csv file through Excel or something that formats the file.


    Can you open it with notepad and copy the exact format of the file into a code block here? It that is an exact copy of the file contents then jrv is right, it's not a CSV file and you'll need to take a different approach. If it is what you get in excel then it's a rendering of a csv file and it may still be possible to use either jsv or jaap's approaches.

    Thursday, July 18, 2013 9:03 AM
  • Thank you guys, splitting them into a single string fixed the issue. I appreciate all your help and support.
    Thursday, July 18, 2013 8:36 PM
  • Thank You!
    Thursday, July 18, 2013 8:37 PM