Remove Top Line of Text File with PowerShell

后端 未结 10 1258
误落风尘
误落风尘 2020-12-01 11:51

I am trying to just remove the first line of about 5000 text files before importing them.

I am still very new to PowerShell so not sure what to search for or how to

10条回答
  •  我在风中等你
    2020-12-01 12:35

    Inspired by AASoft's answer, I went out to improve it a bit more:

    1. Avoid the loop variable $i and the comparison with 0 in every loop
    2. Wrap the execution into a try..finally block to always close the files in use
    3. Make the solution work for an arbitrary number of lines to remove from the beginning of the file
    4. Use a variable $p to reference the current directory

    These changes lead to the following code:

    $p = (Get-Location).Path
    
    (Measure-Command {
        # Number of lines to skip
        $skip = 1
        $ins = New-Object System.IO.StreamReader ($p + "\test.log")
        $outs = New-Object System.IO.StreamWriter ($p + "\test-1.log")
        try {
            # Skip the first N lines, but allow for fewer than N, as well
            for( $s = 1; $s -le $skip -and !$ins.EndOfStream; $s++ ) {
                $ins.ReadLine()
            }
            while( !$ins.EndOfStream ) {
                $outs.WriteLine( $ins.ReadLine() )
            }
        }
        finally {
            $outs.Close()
            $ins.Close()
        }
    }).TotalSeconds
    

    The first change brought the processing time for my 60 MB file down from 5.3s to 4s. The rest of the changes is more cosmetic.

提交回复
热议问题