Explore BrainMass

Explore BrainMass

    UNIX shell script to search for most occurences of a keyword

    Not what you're looking for? Search our solutions OR ask your own Custom question.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    Write a UNIX shell script that will search current directory for a text file that has the most occurrence of a given keyword, and display it on the terminal.

    For example, if I want to search for a text file/script that contains the most number of for loop statements, and have it displayed on the screen, How would I do that?

    © BrainMass Inc. brainmass.com November 24, 2021, 2:57 pm ad1c9bdddf

    Solution Preview

    Assuming single occurrence of search string (for example, "for") in a line, following simple pipeline will do the job.

    grep -c "for" * | sort -nrt: +1 | head -1

    Above pipeline command ...

    Solution Summary

    Solution first gives a simple shell pipeline to achieve the goal and then shows how it can be put in a script form.