1

I need to count the number of lines in x files and compare which has more.

The one I've done only takes two files and compares them. Any idea how to make it x amount of files?

echo Enter the filename read fn

echo Enter another file read fn1

for WORD in $(cat $fn) do
        echo "$WORD" done | wc -l

for WORD in $(cat $fn1) do
        echo "$WORD" done | wc -l

if (cat $fn | wc -l > cat $fn1 | wc -l) then
        echo First file has more lines than second file else if (cat $fn1 | wc -l > cat $fn | wc -l) then
        echo Second file has more lines than first file.

fi
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175

4 Answers4

4
find . -name "*.txt" -exec wc -l '{}' \; | sort -n

you can learn line size then sort them with a one liner.

Selman Ulug
  • 161
  • 4
1
find . -name '*txt' | xargs wc -l | sort -n

On my machine, this was faster than the -exec version.

Eric Wilson
  • 4,622
  • 9
  • 32
  • 43
1
wc -l * | head -n -1 | sort | tail -1 | cut -d ' ' -f 3

This will give you the filename of the file with the highest number of lines

Khaja Minhajuddin
  • 857
  • 2
  • 12
  • 16
1

Just name all files you want to compare, and sort by size (-numeric):

wc -l a.html b.html c.html | sort -n
user unknown
  • 10,267
  • 3
  • 35
  • 58