Remove Top Line of Text File with PowerShell

后端 未结 10 1239
误落风尘
误落风尘 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:22

    Another approach to remove the first line from file, using multiple assignment technique. Refer Link

     $firstLine, $restOfDocument = Get-Content -Path $filename 
     $modifiedContent = $restOfDocument 
     $modifiedContent | Out-String | Set-Content $filename
    
    0 讨论(0)
  • 2020-12-01 12:24

    Using variable notation, you can do it without a temporary file:

    ${C:\file.txt} = ${C:\file.txt} | select -skip 1
    
    function Remove-Topline ( [string[]]$path, [int]$skip=1 ) {
      if ( -not (Test-Path $path -PathType Leaf) ) {
        throw "invalid filename"
      }
    
      ls $path |
        % { iex "`${$($_.fullname)} = `${$($_.fullname)} | select -skip $skip" }
    }
    
    0 讨论(0)
  • 2020-12-01 12:29

    For smaller files you could use this:

    & C:\windows\system32\more +1 oldfile.csv > newfile.csv | out-null

    ... but it's not very effective at processing my example file of 16MB. It doesn't seem to terminate and release the lock on newfile.csv.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题