none
Power Shell - Read from text file RRS feed

  • Question

  • Looking to see how to output select data from c:\test\test.txt file to c:\test\output.txt based on the following condition.

    Select data from text file that has a category of "Salads" and only return the name of the Salad to a text file.

    test.txt contains the following data

    1062,9971,Small Salad,,"40:""Salads"
    1063,9972,Large Pizza,,"60:""Pizzas"
    1065,9972,Large Salad,,"40:""Salads"

    Desired output

    Small Salad

    Large Salad

    Thursday, October 2, 2014 4:38 PM

Answers

  • Here's a couple options. Import the file as a CSV file, but only import the first three sections between commas. For instance, the first line of your test.txt file would import as 1062, 9971, and Small Salad. Then check for the word salad and write it out if it exists. Here's an example.

    $File = Import-Csv -Path C:\test\test.txt -Header One,Two,Three
    
    Foreach ($Entry in $File) {
        If ($Entry.Three -like '*salad') {
            Write-Output $Entry.Three
        }
    }

    As well, you could import all the contents of the file by changing the -Header parameter to One,Two,Three,Four,Five. Doing this, however, will mangle the data in Five because of the double quotes: "40:""Salads" would become 40:"Salads. Then you could do this the way to wanted to do this, by checking the category of salads. Here's an example of this, as well.

    $File = Import-Csv -Path C:\test\test.txt -Header One,Two,Three,Four,Five
    
    Foreach ($Entry in $File) {
        If ($Entry.Five -like '*salads') {
            Write-Output $Entry.Three  
        }
    }
    Update: There are ways to read it in as a text file using Get-Content and then scanning each line, but because your source file has comma deliminators, it makes better sense to read it in as a CSV. To write this to a file, replace the Write-Output command with Add-Content -Value $Entry.Three -Path C:\test\output.txt

    • Edited by tommymaynard Thursday, October 2, 2014 6:04 PM
    • Marked as answer by David Keane Friday, October 3, 2014 5:43 PM
    Thursday, October 2, 2014 5:49 PM

All replies

  • Here's a couple options. Import the file as a CSV file, but only import the first three sections between commas. For instance, the first line of your test.txt file would import as 1062, 9971, and Small Salad. Then check for the word salad and write it out if it exists. Here's an example.

    $File = Import-Csv -Path C:\test\test.txt -Header One,Two,Three
    
    Foreach ($Entry in $File) {
        If ($Entry.Three -like '*salad') {
            Write-Output $Entry.Three
        }
    }

    As well, you could import all the contents of the file by changing the -Header parameter to One,Two,Three,Four,Five. Doing this, however, will mangle the data in Five because of the double quotes: "40:""Salads" would become 40:"Salads. Then you could do this the way to wanted to do this, by checking the category of salads. Here's an example of this, as well.

    $File = Import-Csv -Path C:\test\test.txt -Header One,Two,Three,Four,Five
    
    Foreach ($Entry in $File) {
        If ($Entry.Five -like '*salads') {
            Write-Output $Entry.Three  
        }
    }
    Update: There are ways to read it in as a text file using Get-Content and then scanning each line, but because your source file has comma deliminators, it makes better sense to read it in as a CSV. To write this to a file, replace the Write-Output command with Add-Content -Value $Entry.Three -Path C:\test\output.txt

    • Edited by tommymaynard Thursday, October 2, 2014 6:04 PM
    • Marked as answer by David Keane Friday, October 3, 2014 5:43 PM
    Thursday, October 2, 2014 5:49 PM
  • Is there a typo in your test.txt ? The quote marks look out of place, especially the doubles.

    TechNet

    Thursday, October 2, 2014 5:59 PM
  • I see what you are saying however the format of the text is what I have to work with. I will try the suggested solution above and see what results I get.
    Friday, October 3, 2014 2:07 PM
  • Works a charm! Thank you for your help
    Friday, October 3, 2014 5:43 PM