Working with a txt file, search in string and formatted output

This topic contains 8 replies, has 3 voices, and was last updated by Profile photo of Guido Langendorff Guido Langendorff 1 year, 4 months ago.

  • Author
    Posts
  • #31727
    Profile photo of Guido Langendorff
    Guido Langendorff
    Participant

    I have a txt file, not created by my hand, looking something like this:
    app1 : Not installed
    application2 : Installed
    App3 : Not installed

    The output I'd like to have is
    App1—————-Not Installed

    application2——Installed
    App3—————-Installed

    Script (part off) I have so far:

    $SearchStrings = @("Installed","Not installed")
    Foreach ($SearchTxt in $SearchStrings) {
        $Capture = Select-String -Path $SourceFile -pattern $SearchTxt | Out-File $Results -Append | Format-Table -AutoSize 
    }
    

    This is partly working. I do get a file (aka: $Results) but it holds the complete path/filename and linenumber. To have that eliminated would be sufficient for me.
    But even better it would be if I get the output formatted like in the example.

    I have read to much and as we say in Dutch: can not see the Forrest through the trees anymore. Hope somebody is laughing at me in a kind way and will tell how I should have done it.

  • #31730
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    I may be understanding your question wrong, but it looks like you are just wanting to remove the colon and a space?

    $input = @"
    app1 : Not installed
    application2 : Not installed
    App3 : Installed
    "@
    
    $input -replace ": " | 0ut-file C:\outputfile.txt
    

    Results:
    app1 Not installed
    application2 Not installed
    App3 Installed

  • #31735
    Profile photo of Guido Langendorff
    Guido Langendorff
    Participant

    Wow, is it that simple? I have to reset my Google search patterns/habits 😉
    I will work with this later today. Thx.

    I have changed my original txt.
    The thing that was not clear is I need the output in columns like
    Application————————– Installed (where — are spaces)

  • #31740
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    You can still use Curtis's solution, but instead of replacing the colon and space with nothing replace it with a tab, using `t. e.g. to add two tabs.

    $input -replace ": ","`t`t" | Out-file F:\__temp\outputfile.txt
    
  • #31742
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Matt, that may work or may not visually. If one application's name is longer than the others, the tabs will not space evenly. I would be better to throw each value into a powershell object and then let powershell format it.

    In the example below I just look at each line from the input and split it as it seems space colon space " : " is consistently delimiting each value set. Then I assign the first value as the application and the second as the status in a powershell custom object.

    $input = "app1 : Not installed", "application2 : Not installed", "App3 : Installed"
    
    $input | 
    ForEach-Object {
        $line = $_ -split " : "
        [PSCustomObject]@{
            'Application' = $line[0]
            'Status' = $line[1]
        }
    } | 
    Format-Table -AutoSize
    

    Results:

    Application  Status       
    -----------  ------       
    app1         Not installed
    application2 Not installed
    App3         Installed    
    
  • #31743
    Profile photo of Guido Langendorff
    Guido Langendorff
    Participant

    Sorry, but somewhere I still get stuck..
    So here is the input data (file called "audit.txt")

    OK
    .NET : Skipped - 3.5.SP1.Full
    foobar2000 : Update - 1.3.9
    .NET 4 : Skipped - 4.5.51209
    ..NET 4.6 : Not installed
    Firefox : OK - 42.0
    7-Zip : Not installed
    Flash : OK - 19.0.0.226
    Flash (IE) : Update - 19.0.0.226
    

    and what I need

    .NET                  Skipped - 3.5.SP1.Full
    .NET 4               Skipped - 4.5.51209
    
    Flash (IE)           Update - 19.0.0.226
    foobar2000         Update - 1.3.9
    
    ..NET 4.6           Not installed
    7-Zip                  Not installed
    
    Firefox              OK - 42.0
    Flash                OK - 19.0.0.226
    

    .. grouped and sorted

  • #31744
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    As is, you should get this

    $input = Get-Content audit.txt
    
    $input | 
    ForEach-Object {
        $line = $_ -split " : "
        [PSCustomObject]@{
            'Application' = $line[0]
            'Status' = $line[1]
        }
    } | 
    Format-Table -AutoSize
    

    Result:

    Application Status                
    ----------- ------                
    OK                                
    .NET        Skipped - 3.5.SP1.Full
    foobar2000  Update - 1.3.9        
    .NET 4      Skipped - 4.5.51209   
    ..NET 4.6   Not installed         
    Firefox     OK - 42.0             
    7-Zip       Not installed         
    Flash       OK - 19.0.0.226       
    Flash (IE)  Update - 19.0.0.226
    

    If you want to sort, you just add that to your pipeline

    $input = Get-Content audit.txt
    
    $input | 
    ForEach-Object {
        $line = $_ -split " : "
        [PSCustomObject]@{
            'Application' = $line[0]
            'Status' = $line[1]
        }
    } | 
    Sort-Object -Property Application |
    Format-Table -AutoSize
    

    Results:

    Application Status                
    ----------- ------                
    ..NET 4.6   Not installed         
    .NET        Skipped - 3.5.SP1.Full
    .NET 4      Skipped - 4.5.51209   
    7-Zip       Not installed         
    Firefox     OK - 42.0             
    Flash       OK - 19.0.0.226       
    Flash (IE)  Update - 19.0.0.226   
    foobar2000  Update - 1.3.9        
    OK                                
    

    There is also a group-object cmdlet, but your data set does not contain any group data. None of the values equal each other so they could not be grouped. You would have to add additional code to analysis each line and determine, based on your preset definitions, what group the application falls in and then add that as an additional property in your powershell custom object. Then you can use group-object to group based on that new calculated property.

  • #31745
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Scratch that, I see your grouping now. It's on the status, not the application.

    Get-Content audit.txt | 
    ForEach-Object {
        $line = $_ -split " : "
        $StatusVersion = $line[1] -split " - "
        [PSCustomObject]@{
            'Application' = $line[0]
            'Version' = $StatusVersion[1]
            'Status' = $StatusVersion[0]
        }
    } | 
    Sort-Object -Property Application |
    Group-Object -Property Status |
    Select-Object -ExpandProperty Group |
    Format-Table -AutoSize
    

    Results:

    Application Version      Status       
    ----------- -------      ------       
    ..NET 4.6                Not installed
    7-Zip                    Not installed
    .NET        3.5.SP1.Full Skipped      
    .NET 4      4.5.51209    Skipped      
    Firefox     42.0         OK           
    Flash       19.0.0.226   OK           
    Flash (IE)  19.0.0.226   Update       
    foobar2000  1.3.9        Update       
    OK                                    
    
  • #31770
    Profile photo of Guido Langendorff
    Guido Langendorff
    Participant

    Curtis, you'r the MAN!. Thank you SO much for helping in the solution's and in my training... Gonna study this

You must be logged in to reply to this topic.