Explore BrainMass
Share

Explore BrainMass

    UNIX shell script to search for most occurences of a keyword

    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 October 10, 2019, 12:35 am ad1c9bdddf
    https://brainmass.com/computer-science/searching/unix-shell-script-to-search-for-most-occurences-of-a-keyword-297889

    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.

    $2.19