165

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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

6 Answers6

205

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

Note: /dev/tty is the controlling terminal. From the tty(4) man page:

The file /dev/tty is a character file with major number 5 and minor number 0, usually of mode 0666 and owner.group root.tty. It is a synonym for the controlling terminal of a process, if any.

In addition to the ioctl(2) requests supported by the device that tty refers to, the ioctl(2) request TIOCNOTTY is supported.

AdminBee
  • 22,803
JRFerguson
  • 14,740
  • 22
    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. – glenn jackman Nov 24 '11 at 19:31
  • How to save the result of wc -l < filename into a variable to use later? – Sigur Sep 05 '12 at 23:01
  • 2
    @Sigur : LINES=$(wc -l filename); echo ${LINES} – JRFerguson Sep 06 '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 06 '12 at 22:06
  • Thanks a lot dude... Using your answer 4th time. :)

    i keep forgetting this and ur answer helps me the most

    – Akarsh Satija Oct 01 '15 at 12:58
  • Is there a way (perhaps using tee?) that you can both save the number of lines and have the prints still come out? – Chris Apr 19 '16 at 14:42
  • 1
    @Chris, see https://unix.stackexchange.com/questions/72819/count-number-of-lines-of-output-from-previous-program – shiri Oct 31 '17 at 08:41
  • +1 for teaching me the tee thing. Brilliant. – pgr Sep 12 '18 at 09:54
88

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

robsch
  • 169
peri4n
  • 981
  • 2
    I think that ^ is confusing. Is that the string you are trying to match or something else? – Antonio Mar 03 '15 at 09: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 04 '15 at 07:59
  • 3
    grep -c '' filename will do the trick too. – yumitsu Feb 28 '18 at 18:22
  • 2
    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 – Mickäel A. Jun 02 '20 at 13:27
  • The ^ is an anchor, meaning the start of any line. The corresponding $ anchors to the end of the line. https://www.gnu.org/software/grep/manual/html_node/Anchoring.html - Extremely useful when you need to be precise. – sastorsl Jun 16 '23 at 11:37
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
aioobe
  • 384
5

Using AWK to count the lines in a file

awk 'END {print NR}' filename
4

You can use wc -l to get line count.

Example:

$ cat foobar.txt | wc -l
  • 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. – Sachin Divekar 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)
ling
  • 411