total number of lines in a file

Hi ,

How about find the total number of lines in a file ?
How can i do that with the "grep" command ?

Please dont hijack another thread. I am creating a new thread.

You can use wc to find the number of lines.

wc -l < file.txt

Using grep you can use

grep -c "." file.txt
zsh 4.3.4% cat file
a

b
zsh 4.3.4% wc -l file
3 file
zsh 4.3.4% grep -c . file
2

may be something like:

zsh 4.3.4% grep -c ^ file
3

and it's not bulletproof either ...

Hmm..:confused: my input file had empty lines or so I thought. It had the ^M characters which resulted into the same result for both wc and grep. :frowning:

i find it strange that after joining the forum for so long, you still don't know how to do that?

I suspect homework questions.

Hi ghostdog,

I think i know how to do it. See below! :smiley:

wc -l myfile|awk '{print($1)}'`

So why did you ask for a grep solution ?

Yes,
me also, I use:

ruby -e 'while gets; end; puts $.'<file

instead of:

wc -l<file

Another ways for the fun:

awk 'END {print NR}' file
sed -n '$=' file  # doesn't work for an empty file 

Jean-Pierre.