Extracting one specific line from several txt.files into one txt. file

Hey all,

I hope someone can help me to fix my problem with powershell. I am completely new to that field.

Here ist the problem: I have several txt.files of which I wanted to copy the 13th line of each into one single file. At the moment I am doing this manually but it takes a lot of time doing that.
I attached an image of the specific line of my txt.file I want to copy (its marked in purple and I hope its the 13th line :D)

Currently I work on a mac and I use this code:

  •  cd “here I insert the path to the folder with the txt files”  
    

→ I press enter and insert the next code:

  • Get-ChildItem -path “here I insert the path to the folder with the txt files" | ForEach-Object {(Get-content *.txt -TotalCount 13)[-1]>>out.txt}

If I start directly with the Get-ChildItem code my mac cannot find the path.
This code works and creates an out.txt file with e.g 5 lines from 5 files. However, these 5 lines show the same values and I cannot even find these values in the original txt.file.

If someone could help me with this issue I would be very thankful.

All the best,
Anne

Anne,
Welcome to the forum. :wave:t4:

You were almost there.

$Path = 'folder path'
$OutputFile = 'outputfile including path'
$Result = 
Get-ChildItem -Path $Path -Filter *.txt -File | 
    ForEach-Object { 
        (Get-Content -Path $_.FullName -TotalCount 13)[-1]
    }
$Result | Out-File -FilePath $OutputFile

When you provide complete paths for the variables it does not matter where you save the script or what your current working directory is. :wink:

Hey Olaf,

thank you a million times for your help and time !!! It worked immediately :slight_smile: You literally saved my life.

All the best,

Anne

Great. :+1:t4: I’m glad it helped. :slightly_smiling_face:

If your life depends on something like this, you are doing something wrong.
:smirk: :smirk:
:stuck_out_tongue_winking_eye: :stuck_out_tongue_winking_eye: :kissing_heart: