我有一个包含大型csv文件的目录和子目录列表.这些文件中大约有5亿行,每行都是一条记录.我想知道
>每个文件中有多少行.
>目录中有多少行.
>总共有多少行
最重要的是,我需要“人类可读的格式”,例如. 12,345,678而不是12345678
如何以3种方式学习如何做到这一点会很好.普通的香草bash工具,awk等,以及perl(或python).
解决方法:
How many lines are in each file.
我相信,使用wc,最初用于字数,但它可以做行,字,字符,字节和最长的行长度. -l选项告诉它计算行数.
wc -l <filename>
这将输出以下行数:
$wc -l /dir/file.txt
32724 /dir/file.txt
您也可以将数据传输到wc:
$cat /dir/file.txt | wc -l
32724
$curl google.com --silent | wc -l
63
How many lines are in directory.
尝试:
find . -name '*.pl' | xargs wc -l
另一个单线:
( find ./ -name '*.pl' -print0 | xargs -0 cat ) | wc -l
BTW,wc命令计算新行代码,而不是行.当文件中的最后一行没有以新行代码结束时,这将不计算在内.
你可以使用grep -c ^,完整的例子:
#this example prints line count for all found files
total=0
find /path -type f -name "*.PHP" | while read FILE; do
#you see use grep instead wc ! for properly counting
count=$(grep -c ^ < "$FILE")
echo "$FILE has $count lines"
let total=total+count #in bash, you can convert this for another shell
done
echo TOTAL LInes COUNTED: $total
How many lines in total
不确定我理解你的要求是否正确.例如这将以下列格式输出结果,显示每个文件的行数:
# wc -l `find /path/to/directory/ -type f`
103 /dir/a.PHP
378 /dir/b/c.xml
132 /dir/d/e.xml
613 total
或者,只输出没有文件的新行字符总数按文件计数到以下命令可以证明是有用的:
# find /path/to/directory/ -type f -exec wc -l {} \; | awk '{total += $1} END{print total}'
613
Most importantly, I need this in ‘human readable format’ eg.
12,345,678 rather than 12345678
printf "%0.2f\n" $T
与往常一样,有许多不同的方法可用于实现此处提到的相同结果.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。