我的目录中有多个文本文件,并且想要相互比较每个文件的行数。
下面是我的代码,但输出不正确;无论行数如何,它总是向我显示“更大”。
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
关于linux - 比较多个文本文件中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53584074/