147

I am redirecting grep results to a file, and then using cat to show its contents on the screen. I want to know how many lines of results I have in my results file and then add it to some counter.

What will be the best way? Any relevant flag to grep or cat?

1
  • and you can use awk for simple linebased stuff, too ...
    – hochl
    Nov 24 '11 at 15:28
179

If you have already collected the grep output in a file, you could output a numbered list with:

cat -n myfile

If you only want the number of lines, simply do:

wc -l myfile

There is absolutely no reason to do:

cat myfile | wc -l

...as this needlessly does I/O (the cat) that wc has to repeat. Besides, you have two processes where one suffices.

If you want to grep to your terminal and print a count of the matches at the end, you can do:

grep whatever myfile | tee /dev/tty | wc -l
8
  • 20
    Note that wc -l filename will output the filename in addition to the number of lines. If you only want the number, use this form wc -l < filename -- since wc is now reading from stdin, there is no filename to print. Nov 24 '11 at 19:31
  • How to save the result of wc -l < filename into a variable to use later?
    – Sigur
    Sep 5 '12 at 23:01
  • 2
    @Sigur : LINES=$(wc -l filename); echo ${LINES}
    – JRFerguson
    Sep 6 '12 at 13:00
  • 1
    @Sigur : I should note that this is POSIX syntax in lieu of the archaic back-ticks often seen; viz. LINES=wc -l filename. Modern shells that are POSIX compliant support the preferred $(...) It's much easier to read, too.
    – JRFerguson
    Sep 6 '12 at 22:06
  • 1
    @Chris, see unix.stackexchange.com/questions/72819/…
    – shiri
    Oct 31 '17 at 8:41
76

The -c flag will do the job. For example:

 grep -c ^ filename

will count the lines returned by grep.

Documented in the man page:

-c, --count Suppress normal output; instead print a count of matching lines for each input file

4
  • 1
    I think that ^ is confusing. Is that the string you are trying to match or something else?
    – Antonio
    Mar 3 '15 at 9:07
  • 3
    ^ is not a string you are matching but the position of a possibly following string. If the string is left out it matches only the position. Which in this case, is the start of a line. So effectively, you are telling grep to count the lines.
    – peri4n
    Mar 4 '15 at 7:59
  • 2
    grep -c '' filename will do the trick too. Feb 28 '18 at 18:22
  • 1
    Piping git log command output to wc -l returned the wrong number of lines (and thus of commits), but grep -c ^ (or grep -c $) returned the correct number. Thanks
    – flawyte
    Jun 2 '20 at 13:27
22

Use

your_command | wc -l

From the manual:

NAME
       wc - print newline, word, and byte counts for each file

...

       -l, --lines
          print the newline counts
5

Using AWK to count the lines in a file

awk 'END {print NR}' filename
1
  • This is better, wc -l gives wrong count sometimes. Sep 5 '18 at 13:40
4

You can use wc -l to get line count.

Example:

$ cat foobar.txt | wc -l
1
  • 12
    This is the common shell mistake Redundant cat. The effect of this is to create a pipe, executing cat file with its output redirected to the pipe and wc -l with its input redirected from the pipe. But you can eliminate the entire invocation of "cat" by just redirecting wc's input to come from the file rather than the pipe. Just do wc -l foobar.txt. Nov 25 '11 at 18:44
0

Don't use wc: it doesn't count the last line if it's not terminated by the end of line symbol (at least on mac). Use this instead:

nbLines=$(cat -n file.txt | tail -n 1 | cut -f1 | xargs)