-3

I have a file that contains multiple commands, ls,echo,ps etc. I want to find with a terminal command which command occurs most (have the most appearances on the file) and then execute man to it. For example my file contain ls ls ps I must execute man ls. The form of the file is : multiple lines and in every line I have a command only.

file example:

ls
ls
ps
echo
man
cp
rm
2

Sort the file, count the number of times each word occurs in sequence, sort again but this time on the number (in decreasing order), grab the first one and remove the number (assuming the original list only contains single words with no whitespace):

sort file | uniq -c | sort -nr | head -n 1 | awk '{ print $2 }'

For the given file, this would produce ls.

To call man for this command:

man "$( sort file | uniq -c | sort -nr | head -n 1 | awk '{ print $2 }' )"

Or just

man "$( awk '++c[$1] && c[$1] > m { mc=$1; m=c[$1] } END { print mc }' file )"

Related:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.