比较多个文本文件中的行数

comparing line count in multiple text files

我在一个目录中有多个文本文件,我想比较每个文件的行数。

下面是我的代码,但输出不正确;无论行数如何,它总是向我显示 "bigger"。

for f in *.txt; do
for f2 in *.txt; do
if [ "wc -l $f" > "wc -l $f2" ]; then 
echo bigger;
fi;
done;done

我不确定这部分 if [ "wc -l $f" > "wc -l $f2" ] 是否正确。

双引号引入字符串,而不是 运行 包含的命令。 [ ... ]中的>比较的是字符串,不是数字,需要反斜杠,否则会被解释为重定向。此外,当文件大小相同时(例如,当 $f == $f1 时),您期望得到什么输出?

#!/bin/bash
for f in *.txt; do
    size_f=$(wc -l < "$f")
    for f2 in *.txt; do
        size_f2=$(wc -l < "$f2")

        if (( size_f > size_f2 )) ; then
            echo "$f" bigger than "$f2"
        elif (( size_f == size_f2 )) ; then
            echo "$f" same size as "$f2"
        else
            echo "$f" smaller than "$f2"
        fi
    done
done

这是你想要的:

#!/bin/bash

N=0
M=0
for line in $( find . -type f -name \*.sh ); do
    N=$( wc -l ${line} | cut -d" " -f1 )
    FILE=$( wc -l ${line} | cut -d" " -f2 )
    if [[ ${M} < ${N} ]]; then
        M=${N}
        BIGFILE=${FILE}
        echo "GOTCHA : ${M}"
    fi
done
echo "BIG FILE: ${BIGFILE} ( ${M} lines )"