How to count the number of lines containing a given sequence of characters in a given file. Suppose there is a file in the home directory info.txt it contains the following lines:

  • linux good
  • linux good
  • linux best
  • linux cool

How to find out the number of lines. Where does the word good meet?

    1 answer 1

    Using grep , we find the necessary lines and count them with wc :

     grep good info.txt | wc -l 
    • Not the first year I use linux on my home PC, but still not used to how simple specific tasks are solved. - aryndin
    • 3
      @ jumpjet67: grep -c -w good info.txt - jfs
    • @jfs is already slightly non-UNIX-way - "the program solves only one problem, but does it as efficiently as possible.", although I am not in favor of extremes. - aryndin
    • @ jumpjet67 -w finds only whole words (the natural part of grep , which wc -l does not implement). The established functionality is useful to include in the program in order not to implement from scratch every time, for example, this allows the return status to be set if -c returns zero. If you rarely have to use -c , then of course it’s good to know about wc -l since you can use it with other commands. Another example: sort -u and uniq . - jfs