Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

migrated from stackoverflow.com Nov 25 '11 at 17:29

This question came from our site for professional and enthusiast programmers.

and you can use awk for simple linebased stuff, too ... – hochl Nov 24 '11 at 15:28
up vote 87 down vote accepted

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
share|improve this answer
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 5 '12 at 23:01
@Sigur : LINES=$(wc -l filename); echo ${LINES} – JRFerguson Sep 6 '12 at 13:00
@JRFerguson, thanks. It works. – Sigur Sep 6 '12 at 21:20
@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

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

 grep -c ^ filename

will count the lines returned by grep

share|improve this answer
I think that ^ is confusing. Is that the string you are trying to match or something else? – Antonio Mar 3 '15 at 9:07
^ 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


your_command | wc -l

From the manual:

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


       -l, --lines
          print the newline counts
share|improve this answer

You can use wc -l to get line count.


$ cat foobar.txt | wc -l
share|improve this answer
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

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)
share|improve this answer

Using AWK to count the lines in a file

awk 'END {print NR}' filename
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.