32

嗨,我是 UNIX 新手,我必须从传入的 csv 文件中获取行数。我使用以下命令来获取计数。

wc -l filename.csv

考虑带有 1 条记录的文件,如果我发出相同的命令,我会在开始时获取一些带有 * 的文件,并且对于这些文件,如果我发出相同的命令,我会得到计数为 0。 * 在这里是否意味着任何东西,如果我得到一个带有 ctrlm(CR) 而不是的文件NL 如何获取该文件中的行数。给我一个解决问题的命令。提前致谢。

4

4 回答 4

65

以下查询可帮助您获取计数

cat FILE_NAME  | wc -l
于 2015-01-08T13:53:43.347 回答
24

所有的答案都是错误的。CSV 文件接受引号之间的换行符,但仍应将其视为同一行的一部分。如果你的机器上有 Python 或 PHP,你可能会做这样的事情:

Python

//From stdin
cat *.csv | python -c "import csv; import sys; print(sum(1 for i in csv.reader(sys.stdin)))"

//From file name
python -c "import csv; print(sum(1 for i in csv.reader(open('csv.csv'))))"

PHP

//From stdin
cat *.csv | php -r 'for($i=0; fgetcsv(STDIN); $i++);echo "$i\n";'

//From file name
php -r 'for($i=0, $fp=fopen("csv.csv", "r"); fgetcsv($fp); $i++);echo "$i\n";'

我还创建了一个脚本来模拟以下输出wc -lhttps ://github.com/dhulke/CSVCount

于 2018-11-29T22:04:47.090 回答
12

如果.csv同一文件夹中有多个文件,请使用

cat *.csv | wc -l

csv获取当前目录中所有文件的总行数。因此, -c计数字符和-m计数字节(只要您使用 ASCII 就相同)。您还可以使用wc来计算文件的数量,例如:ls -l | wc -l

于 2018-10-12T20:20:36.870 回答
7

wc -l 我的文本文件

或者只输出行数:

wc -l <​​我的文本文件

Usage: wc [OPTION]... [FILE]...
or:  wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  With no FILE, or when FILE is -,
read standard input.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
  --files0-from=F    read input from the files specified by
                       NUL-terminated names in file F;
                       If F is - then read names from standard input
  -L, --max-line-length  print the length of the longest line
  -w, --words            print the word counts
  --help     display this help and exit
  --version  output version information and exit
于 2016-11-08T23:39:20.193 回答