4

I'm running Windows 7 and have GnuWin32. I have a several-gigabyte text file with LF (\n) line endings. I want to know how many lines it has (or alternatively how many LFs it has, which is, let's say, one fewer, but I don't care). What's the fastest or least expensive way to get my answer?

Some possibilities (though I'm sure I'm missing some):

  • wc -l foo
  • grep -c $ foo (with -c, prints only the count of matching lines)
  • grep -c ^^ foo (the first caret escapes the second)
  • sed -n $= foo (-n prevents printing the line; $ restricts to the last line; = prints the line number)

(Those are the GnuWin32 utilities. I don't know of any native-to-Windows way.)

msh210
  • 225
  • 3
  • 15

2 Answers2

4

Windows command line solution

type foo | find "" /v /c

Powershell solutions

(get-content foo | measure-object -line).lines

(dir foo | select-string .).count

(type foo).count

(gc foo | measure-object | select count).count

perl solution

perl -pe '}{$_=$.' foo

awk solution

awk 'END { print NR }' foo

Further reading

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
0

The fastest for me is the command line: find /v /c "" foo.txt

I am yet to do a proper benchmark but I tried type and (get-content foo | measure-object -line) for a 10GB file on a 2GB ram server, but they took far too long to complete. Find however returned fast enough

Chibueze Opata
  • 293
  • 1
  • 2
  • 10
  • 1
    You propose a solution which is already suggested. Please pay an answer only if it provides additional information. Have you compared multiple ways? Which ones? What was the result? What was your test case? ... – Máté Juhász Oct 31 '18 at 08:55
  • @MátéJuhász I didn't find it elsewhere, `find` command is not the same as `type`. Posted in a hurry but will update answer with more details. – Chibueze Opata Oct 31 '18 at 12:00