Read text and copy to new text file

This topic contains 5 replies, has 4 voices, and was last updated by  Rob Simmers 2 years, 6 months ago.

  • Author
    Posts
  • #25807

    Wilfredo Perez
    Participant

    I have a text file with some data between line. like so:
    _________________________________________________________
    XXXXXXXXXXXXX
    XXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXXXXXXXXXX
    _______________________________________________________________
    XXXXXXXXXXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    _________________________________________________________________

    How can I extract the data between the lines ans create a new Text file with the extracted data.

  • #25816

    Peter Jurgens
    Participant

    What kind of character is the horizontal line? Is it just dashes? Doesn't look so from your sample text but it would be important to know. I would suggest using a simple where-object statement to select the data from lines that aren't the horizontal line, or matching the actual data on the lines you're trying to keep.

    Get-Content c:\file.txt | Where-Object {$_ -match "X"} | Out-File c:\output.txt
    

    That's based on your sample content of course, considering it's all X's 🙂

  • #25818

    Tim Pringle
    Participant

    If you just want to ignore the lines, something like this would be what you need.

    #requires -Version 1
    Get-Content -Path C:\temp\file.txt |
    Where-Object -FilterScript {
        $_ -notlike '--*'
    } |
    Out-File -FilePath C:\Temp\newfile.txt
    
  • #25847

    Wilfredo Perez
    Participant

    The text file I have looks like:

    ____________________________________________

    Data: Data
    Data: Data
    Data: Data
    ________________________________________________

    Data: Data
    Data: Data
    Data: Data
    _________________________________________________

    How can I get just the first set of data and save it into a new text file and do the same for the second set and save the new text file

  • #25848

    Wilfredo Perez
    Participant

    I have this code. which does the job but I need to loop through the text file.

    Get-Content -Path 'C:\Test\FinancialStatements\Perez, Wilfredo 5-19-2015.txt' | 
        Select-String 'Transaction Type:'  -Context 0,15 | 
            Select-Object -First 1 | out-file 'C:\Test\FinancialStatements\OutPut.txt'
    
  • #25851

    Rob Simmers
    Participant

    @Fred

    This is post # 4 by my count for the exact same question:

    My last solution in https://powershell.org/forums/topic/read-text-file-and-find-match/ parsed the files the way you asked with the example dataset you provided, so I'm unsure why you are creating yet another post of the exact same thing. If something is not working as expected, tell us what is not working so that we can work from the same requirements and see the current used solutions that were provided to you. Just starting the same question over and over with different requirements is just wasting everyone's time. Provide all of the requirements, example data to be parsed, expected results in one post so that the community can assist you with your question(s).

You must be logged in to reply to this topic.