Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Mon 20 Aug 2018, 00:31
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Find out how many lines of text is in a text file? (Solved)
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [12 Posts]  
Author Message
LazY Puppy


Joined: 21 Nov 2014
Posts: 2007
Location: Germany

PostPosted: Wed 13 May 2015, 19:34    Post subject:  Find out how many lines of text is in a text file? (Solved)
Subject description: Any solution without to read the file and counting lines?
 

The Title and sub-title says it all.

Want to do this in bash script.

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) Laughing

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

Last edited by LazY Puppy on Wed 13 May 2015, 20:47; edited 1 time in total
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 5117
Location: Ontario

PostPosted: Wed 13 May 2015, 20:04    Post subject:  

There is a command that numbers the lines in a text file. I believe it's
grep -n

Quote:
-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.


Quote:
-l, --lines
print the newline counts



_______________________________________
Back to top
View user's profile Send private message 
LazY Puppy


Joined: 21 Nov 2014
Posts: 2007
Location: Germany

PostPosted: Wed 13 May 2015, 20:47    Post subject:  

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) Laughing

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! Wink
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1579
Location: Japan

PostPosted: Tue 20 Mar 2018, 00:47    Post subject:  

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:
grep -c . filename


For counting all lines (not newline characters) , this should do:
Code:
grep -c ^ filename
Back to top
View user's profile Send private message 
Flash
Official Dog Handler


Joined: 04 May 2005
Posts: 12945
Location: Arizona USA

PostPosted: Tue 20 Mar 2018, 18:06    Post subject:  

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.
Back to top
View user's profile Send private message 
drunkjedi


Joined: 24 May 2015
Posts: 897

PostPosted: Wed 21 Mar 2018, 13:18    Post subject:  

MochiMoppel wrote:
For counting all non-blank lines of a text file I propose
Code:
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.
Back to top
View user's profile Send private message 
musher0


Joined: 04 Jan 2009
Posts: 12578
Location: Gatineau (Qc), Canada

PostPosted: Wed 21 Mar 2018, 19:32    Post subject:  

Hi all.

For fun,
-- with awk:
Code:
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:
while read line;do [ "${#line}" -gt "0" ] && echo "$line";done < text.txt | awk 'END{print NR}'

BFN.

_________________
musher0
~~~~~~~~~~
Siempre será canción nueva... (V. Jara, Manifiesto)
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1579
Location: Japan

PostPosted: Wed 21 Mar 2018, 23:17    Post subject:  

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:
geany_textlines_count.png
 Description   
 Filesize   53.94 KB
 Viewed   115 Time(s)

geany_textlines_count.png

Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1579
Location: Japan

PostPosted: Wed 21 Mar 2018, 23:50    Post subject:  

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:
awk 'NF > 0' text.txt | awk 'END{print NR}'

Less funny but faster:
Quote:
awk '/./ {c++} END {print c}' text.txt
It's still slower than grep.
Back to top
View user's profile Send private message 
drunkjedi


Joined: 24 May 2015
Posts: 897

PostPosted: Wed 21 Mar 2018, 23:53    Post subject:  

AAh, regular expressions....
Thanks MochiMoppel
Back to top
View user's profile Send private message 
musher0


Joined: 04 Jan 2009
Posts: 12578
Location: Gatineau (Qc), Canada

PostPosted: Thu 22 Mar 2018, 13:56    Post subject:  

Why are they called "regular" expressions, anyway?
"Hello" and "Good bye" are regular expressions too! Laughing
(Just being silly.)

_________________
musher0
~~~~~~~~~~
Siempre será canción nueva... (V. Jara, Manifiesto)
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2641

PostPosted: Mon 26 Mar 2018, 13:07    Post subject:  

regular=rule-based
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [12 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1332s ][ Queries: 14 (0.0162s) ][ GZIP on ]