2010-04-06 16 views

Respuesta

20

También puede utilizar la propiedad readcount excluir líneas:

get-content d:\testfile.txt | where {$_.readcount -lt 3 -or $_.readcount -gt 7} 
7

Si necesito para seleccionar sólo algunas líneas, lo haría directamente índice en la matriz:

$x = gc c:\test.txt 
$x[(0..2) + (8..($x.Length-1))] 

También es posible crear una función Skip-Objects

function Skip-Object { 
    param(
     [Parameter(Mandatory=$true,ValueFromPipeline=$true)][PsObject]$InputObject, 
     [Parameter(Mandatory=$true)][int]$From, 
     [Parameter(Mandatory=$true)][int]$To 
    ) 

    begin { 
     $i = -1 
    } 
    process { 
     $i++ 
     if ($i -lt $from -or $i -gt $to) { 
      $InputObject 
     } 
    } 
} 

1..6 | skip-object -from 1 -to 2 #returns 1,4,5,6 
'a','b','c','d','e' | skip-object -from 1 -to 2 #returns a, d, e 
4

El PowerShell Community Extensions viene con un Skip -Object cmdlet:

PS> 0..10 | Skip-Object -Index (3..7) 
0 
1 
2 
8 
9 
10 

Tenga en cuenta que el índice parame ter está basado en 0.

2

Del mismo modo, sin extensiones (tenga en cuenta que -skip necesita el número de elementos para saltar, y no un índice)

$content = get-content d:\testfile.txt 
($content | select -first 3), ($content | select -skip 8) 
Cuestiones relacionadas