none
Foreach Output to CSV RRS feed

  • Question

  • Hi developer,

    I have a written a script but i have no idea how to the foreach output loop to csv file.

    Script is below:

    $regex = "([A-Za-z])\w+|\d+"
    $matchInfo = @((Get-Content \test.txt) |Select-String -Pattern  $regex -AllMatches))
    
    $output = foreach($minfo in $matchInfo)
    {
        foreach($match in @($minfo.Matches | Foreach {$_.Groups[0].value}))
        {
            Write-host -NoNewline "$match,"
        }
        Write-Host ""
    }
    
    $output | ???????
    

    I tried this:
    $output | Out-File -FilePath Test.csv -Encoding OEM

    So i opened the Test.csv and no data. it is empty.

    Any Idea?
    Thanks




    • Edited by IamGuy84 Wednesday, May 20, 2015 9:24 AM
    Wednesday, May 20, 2015 7:42 AM

Answers

  • When you use the command Write-Host,it does only write the text on the shell console... And the variable $output is $null !

    You must store the result in the variable before displaying and saving it. 

    Below a very simple example:

     

    # Store the result in a variable.

    $output= foreach($i in 1..20)

    {

        "value $i";

    }

     

    # Display the result in the "Shell Console"

    $output

     

    # Store the result in a text file.

    $output | Out-File C:\output.txt 

    • Marked as answer by IamGuy84 Thursday, May 21, 2015 8:49 AM
    Wednesday, May 20, 2015 1:01 PM
  • It depends on what you really want to achieve, below you will find different possibilities.

     

    # -- Display the collection in one line (With my previous example)

    "$output"

     

    # -- Break the collection in multiple line (The Output is a [System.Array])

     

    # Store the result in a variable.

    [Array]$output = foreach($i in 1..5)

    {

        # Build the row

        [Array]$row = foreach($j in 1..5)

        {

            "[$i.$j]";

        }

     

        # Add the row to the collection

        $row -join " ";

    }

     

    # Display the result

    $output

     

    # -- Break the collection in multiple line (The Output is a [System.String])

    [String]$output = "";

     

    foreach($i in 1..5)

    {

        # Build the row

        [String]$row = foreach($j in 1..5)

        {

            "[$i.$j]";

        }

     

        # Insert a single line break

        [String]$output += "$row`r`n";

    }

     

    # Display the result

    $output 

    • Marked as answer by IamGuy84 Thursday, May 21, 2015 8:49 AM
    Thursday, May 21, 2015 8:19 AM

All replies

  • Hi,

    do you get any console output from the Write-Host when the script is running?

    I have no idea why your code is not running.

    Try this:

    $regex = .......
    $matchInfo = @((Get-Content \.text.txt) |Select-String -Pattern  $regex -AllMatches))
    
    foreach($minfo in $matchInfo)
    {
        foreach($match in @($minfo.Matches | Foreach {$_.Groups[0].value}))
        {
            "$match," | Out-File -FilePath Test.csv -Encoding OEM -Append
        }
        "" | Out-File -FilePath Test.csv -Encoding OEM -Append
    }


    • Edited by psott Wednesday, May 20, 2015 9:29 AM
    Wednesday, May 20, 2015 9:29 AM
  • Here is my sample complete Script:

    $regex = "([A-Za-z])\w+|\d+"
    $matchInfo = @(Get-Content .\test.txt|Select-String -Pattern  $regex -AllMatches)
    
    $output= foreach($minfo in $matchInfo)
    {
        foreach($match in @($minfo.Matches | Foreach {$_.Groups[0].value}))
        {
            Write-host -NoNewline "$match,"
        }
        Write-Host ""
    }
    Where test.txt:
    aa bb cc 12
    cc dd ee 13
    ff gg hh 15
    ii jj kk 22
    ll mm nn 56

    Output in powershell command:
    aa,bb,cc,12,
    cc,dd,ee,13,
    ff,gg,hh,15,
    ii,jj,kk,22,
    ll,mm,nn,56,

    i tried this as below are not working:
    $Output | out-file -FilePath test.csv -Encoding OEM
    $Output | out-file -FilePath test.txt
    $Output | out-gridview

    Wednesday, May 20, 2015 10:01 AM
  • When you use the command Write-Host,it does only write the text on the shell console... And the variable $output is $null !

    You must store the result in the variable before displaying and saving it. 

    Below a very simple example:

     

    # Store the result in a variable.

    $output= foreach($i in 1..20)

    {

        "value $i";

    }

     

    # Display the result in the "Shell Console"

    $output

     

    # Store the result in a text file.

    $output | Out-File C:\output.txt 

    • Marked as answer by IamGuy84 Thursday, May 21, 2015 8:49 AM
    Wednesday, May 20, 2015 1:01 PM
  • How to avoid no new line without write-host?

    For example:
    value 1
    value 2
    value 3
    value 4
    value 5
    value 6
    value 7
    value 8
    value 9

    To
    value 1 value 2 value 3 value 4 value 5 value 6 value 7 value 8 value 9

    • Edited by IamGuy84 Thursday, May 21, 2015 4:44 AM
    Thursday, May 21, 2015 4:43 AM
  • $output -join ' '

    or

    $output -join ';'
    The below command is to get help
    help about_Join



    Regards Chen V [MCTS SharePoint 2010]

    Thursday, May 21, 2015 8:19 AM
  • It depends on what you really want to achieve, below you will find different possibilities.

     

    # -- Display the collection in one line (With my previous example)

    "$output"

     

    # -- Break the collection in multiple line (The Output is a [System.Array])

     

    # Store the result in a variable.

    [Array]$output = foreach($i in 1..5)

    {

        # Build the row

        [Array]$row = foreach($j in 1..5)

        {

            "[$i.$j]";

        }

     

        # Add the row to the collection

        $row -join " ";

    }

     

    # Display the result

    $output

     

    # -- Break the collection in multiple line (The Output is a [System.String])

    [String]$output = "";

     

    foreach($i in 1..5)

    {

        # Build the row

        [String]$row = foreach($j in 1..5)

        {

            "[$i.$j]";

        }

     

        # Insert a single line break

        [String]$output += "$row`r`n";

    }

     

    # Display the result

    $output 

    • Marked as answer by IamGuy84 Thursday, May 21, 2015 8:49 AM
    Thursday, May 21, 2015 8:19 AM
  • Working like a charm
    Thank you so much.
    Thursday, May 21, 2015 8:49 AM