【问题标题】:Need a command that will separate the count accounting to files that are < 1M lines and > 1M lines需要一个命令将计数记帐与 < 1M 行和 > 1M 行的文件分开
【发布时间】:2026-01-03 03:35:01
【问题描述】:

环境:Solaris 9

我有一个命令可以提供文件总数。但是我需要一个命令来分别计算少于 1M 行的文件和超过 1M 行的文件。我该怎么做?

find . -type f -exec wc -l {} \; | awk '{print $1}' | paste -sd+ | bc

【问题讨论】:

  • 您实际上需要两个命令,对吗?一个生成超过 1M 的文件列表,一个生成
  • 请注意,如果您的find 版本支持它,那么使用+ 代替\; 是明智的。

标签: bash shell awk solaris


【解决方案1】:

使用 -size 选项:

echo "Smaller: $(find . -type f -size -1M | wc -l)"
echo "Larger: $(find . -type f -size +1M | wc -l)"

当你的find不支持1M的时候,写完整的数字就行了。

【讨论】:

    【解决方案2】:

    编辑:@rojomoke 的评论,我这里有一个版本,它使用 wc 实用程序计算文件中的 LINES,因为这是您在原始帖子中使用的内容

    代码:

    # here I am already in the directory with the files so I just use * 
    # to refer to all files
    # the wc -l will return a single column of counts so I use $1 to 
    # refer to field 1
    wc -l * | awk '$1>1e6{bigger++}$1<1e6{smaller++}END{print "Files > 1M lines = ", bigger, "\nFiles < 1M lines = ", smaller}'
    

    输出:

    "Files > 1M lines =  454" 
    "Files < 1M lines =  528"
    

    【讨论】:

    • OP 对wc -l 的使用让我觉得他对包含或多或少 1M 而不是字符的文件感兴趣
    • @rojomoke 谢谢,如果那是 OP 需要的,我已将您的评论用于计算行数而不是字节数,措辞有点不清楚
    最近更新 更多