Find out how many lines of text is in a text file? (Solved)

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

Find out how many lines of text is in a text file? (Solved)

#1 Post by LazY Puppy »

The Title and sub-title says it all.

Want to do this in bash script.

Thanks
Last edited by LazY Puppy on Thu 14 May 2015, 00:47, edited 1 time in total.
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#2 Post by don570 »

There is a command that numbers the lines in a text file. I believe it's
grep -n
-n, --line-number
Prefix each line of output with the line number within its input
file.
Then read the last line in your text file and take first word.

another command is 'wc -l' however you have to be
careful about line ending characters. I think it can be fooled to think
that the file has one more line than it really has.

-l, --lines
print the newline counts

_______________________________________

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#3 Post by LazY Puppy »

Code 'wc -l' is working fine.

Thanks!
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#4 Post by MochiMoppel »

Sorry for reviving this old thread, but why was it marked "Solved"?

Even if LazyPuppy finds that 'wc -l' is working fine, it does not answer the question of how many lines of text are in a text file.

wc -l at best counts the number of all lines in a file, including blank lines. Actually it doesn't even count lines, it counts the number of newline characters. So if a file contains a single line of text, not terminated by a newline character, wc -l will output 0.

For counting all non-blank lines of a text file I propose

Code: Select all

grep -c . filename
For counting all lines (not newline characters) , this should do:

Code: Select all

grep -c ^ filename

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#5 Post by Flash »

He did say 'any solution' though he preferred a Bash script, so here's another one: (In Rox) right click on the file and choose Open as text. The file will open in Geany. Then go to View -> Show line numbers and Geany numbers the lines.

User avatar
drunkjedi
Posts: 882
Joined: Mon 25 May 2015, 02:50

#6 Post by drunkjedi »

MochiMoppel wrote:For counting all non-blank lines of a text file I propose

Code: Select all

grep -c . filename
Hi MochiMoppel,
As I understand "grep -c ." prints only a count of matching lines which contains "."
Am I correct?
Why does it counts correctly even when I have no "." in any line?
This doesn't happen with other alphabets.
If I type "grep -c a" it will only print count of matching lines which contains "a" exactly.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#7 Post by musher0 »

Hi all.

For fun,
-- with awk:

Code: Select all

awk 'NF > 0' text.txt | awk 'END{print NR}'
I.e., silently print lines that have fields in them and then show the number of the last line.

-- to complicate things a little!

Code: Select all

while read line;do [ "${#line}" -gt "0" ] && echo "$line";done < text.txt | awk 'END{print NR}'
BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#8 Post by MochiMoppel »

Flash wrote:The file will open in Geany. Then go to View -> Show line numbers and Geany numbers the lines.
Or go to Tools -> Word Count

To return the number of lines that are not empty it needs a bit more typing:
Attachments
geany_textlines_count.png
(53.94 KiB) Downloaded 133 times

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#9 Post by MochiMoppel »

drunkjedi wrote:As I understand "grep -c ." prints only a count of matching lines which contains "."
Am I correct?
Sort of. What you also need to understand is that grep always expects search patterns to be regular expressions and that a "." in a regex has a special meaning and stands for any character..
The letter "a" has no special meaning and therefore stands for ... well, the letter "a" .
musher0 wrote:For fun,
-- with awk:

Code: Select all

awk 'NF > 0' text.txt | awk 'END{print NR}'
Less funny but faster:
awk '/./ {c++} END {print c}' text.txt
It's still slower than grep.

User avatar
drunkjedi
Posts: 882
Joined: Mon 25 May 2015, 02:50

#10 Post by drunkjedi »

AAh, regular expressions....
Thanks MochiMoppel

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#11 Post by musher0 »

Why are they called "regular" expressions, anyway?
"Hello" and "Good bye" are regular expressions too! :lol:
(Just being silly.)
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#12 Post by amigo »

regular=rule-based

Post Reply