ca9163d9
ca9163d9

Reputation: 29159

select -first 1 on a large file

I am trying to run the following command on a very large text file. However, it's very slow

((cat largefile.txt | select -first 1).split(",")).count()

Is an alternative fast way in powershell? It seems the command will scan the whole file no matter what.

Upvotes: 8

Views: 6649

Answers (2)

Arithmomaniac
Arithmomaniac

Reputation: 4794

It's worse than that - it will load the whole file and turn it into a string array.

Use the native .NET libraries to load just the first line:

$reader = [System.IO.File]::OpenText("largefile.txt")
$line = $reader.ReadLine()
$reader.Close()

(borrowed from How to process a file in Powershell line-by-line as a stream)

Upvotes: 10

jon Z
jon Z

Reputation: 16616

To only get the first x number of lines in a text file, use the –totalcount parameter:

((Get-Content largefile.txt -totalcount 1).split(",")).count

Upvotes: 12

Related Questions