M
M
maniac_by2018-10-05 22:32:02
linux
maniac_by, 2018-10-05 22:32:02

How to find 10 files with the most frequent match for a word?

Hello. There is a task in which you need to find 10 files with the most frequent repetition of words through the terminal.
Searching for one word among files is easy:
find / | xargs grep "penguin" | wc -w
But then I somehow blunted. Any ideas?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2018-10-05
@maniac_by

As an option:

for i in $(find ./ -type f);do  echo $i, $(cat $i | grep -o "penguin" | wc -w); done | sort -k2 -r | head -n10

V
vman, 2018-10-05
@vman

for w in "for" "if" "continue"; do tmp=$(mktemp); grep -P "\b$w\b" * -R | grep -v 'Binary file' | awk -F":" '{print $1}' > $tmp; echo "word: $w"; cat $tmp | sort | uniq -c | sort -hr | head -n 10; rm -f $tmp; done

word: for
    471 __init__.py
    141 depsolve.py
    127 rpmsack.py
    104 sqlitesack.py
     93 packageSack.py
     88 comps.py
     81 history.py
     74 packages.py
     73 yumRepo.py
     58 updateinfo.py
word: if
   1024 __init__.py
    317 yumRepo.py
    297 packages.py
    248 depsolve.py
    235 rpmsack.py
    233 sqlitesack.py
    189 history.py
    155 misc.py
    149 pgpmsg.py
    116 config.py
word: continue
    144 __init__.py
     47 depsolve.py
     35 rpmsack.py
     30 sqlitesack.py
     19 yumRepo.py
     16 history.py
     14 updateinfo.py
     14 transactioninfo.py
     14 packages.py
     14 misc.py

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question