简体   繁体   中英

Faster and precise way to count lines other than wc -l

Usually I use wc -l to count the lines of a file. However for a file with 5*10^7 lines, I get only 10^7 as an answer. I've tried everything proposed here here: How to count lines in a document? But it takes to much time than wc -l .

Is there any other option?

Anyone serious about speed line counting can just create their own implementation:

#include <stdio.h>
#include <string.h>
#include <fcntl.h>

#define BUFFER_SIZE (1024 * 16)
char BUFFER[BUFFER_SIZE];

int main(int argc, char** argv) {
    unsigned int lines = 0;
    int fd, r;

    if (argc > 1) {
        char* file = argv[1];
        if ((fd = open(file, O_RDONLY)) == -1) {
            fprintf(stderr, "Unable to open file \"%s\".\n", file);
            return 1;
        }
    } else {
        fd = fileno(stdin);
    }

    while ((r = read(fd, BUFFER, BUFFER_SIZE)) > 0) {
        char* p = BUFFER;
        while ((p = memchr(p, '\n', (BUFFER + r) - p))) {
            ++p;
            ++lines;
        }
    }

    close(fd);

    if (r == -1) {
        fprintf(stderr, "Read error.\n");
        return 1;
    }

    printf("%d\n", lines);

    return 0;
}

Usage

a < input
... | a
a file

Example:

# time ./wc temp.txt
10000000

real    0m0.115s
user    0m0.102s
sys     0m0.014s

# time wc -l temp.txt
10000000 temp.txt

real    0m0.120s
user    0m0.103s
sys     0m0.016s

* Code compiled with -O3 natively on a system with AVX and SSE4.2 using GCC 4.8.2.

尝试使用nl,看看会发生什么...

You could try sed

sed -n '$=' file

The = says to print the line number, and the dollar says to only do it on the last line. The -n says not to do too much else.

Or here's a way in Perl, save this as wc.pl and do chmod +x wc.pl :

#!/usr/bin/perl
use strict;
use warnings;

    my $filename = <@ARGV>;
    my $lines = 0;
    my $buffer;
    open(FILE, $filename) or die "ERROR: Can not open file: $!";
    while (sysread FILE, $buffer, 65536) {
        $lines += ($buffer =~ tr/\n//);
    }
    close FILE;
    print "$lines\n";

Run it like this:

wc.pl yourfile

Basically it reads your file in in chunks of 64kB at a time and then takes advantage of the fact that tr returns the number of substitutions it has made after asking it to delete all newlines.

取决于打开文件的方式,但可能可以从STDIN读取文件,从而可以解决此问题:

wc -l < file

You can get the line count using awk as well like below

awk 'END {print NR}' names.txt

(OR) Using while .. do .. done bash loop construct like

CNT=0; while read -r LINE; do (( CNT++ )); done < names.txt; echo $CNT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM