Find out how many lines of text is in a text file? (Solved)
- 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)
The Title and sub-title says it all.
Want to do this in bash script.
Thanks
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:
"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:
There is a command that numbers the lines in a text file. I believe it's
grep -n
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.
_______________________________________
grep -n
Then read the last line in your text file and take first word.-n, --line-number
Prefix each line of output with the line number within its input
file.
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
_______________________________________
- LazY Puppy
- Posts: 1934
- Joined: Fri 21 Nov 2014, 18:14
- Location: Germany
- MochiMoppel
- Posts: 2084
- Joined: Wed 26 Jan 2011, 09:06
- Location: Japan
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
For counting all lines (not newline characters) , this should do:
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
Code: Select all
grep -c ^ filename
Hi MochiMoppel,MochiMoppel wrote:For counting all non-blank lines of a text file I proposeCode: Select all
grep -c . filename
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.
Hi all.
For fun,
-- with awk:I.e., silently print lines that have fields in them and then show the number of the last line.
-- to complicate things a little!
BFN.
For fun,
-- with awk:
Code: Select all
awk 'NF > 0' text.txt | awk 'END{print NR}'
-- to complicate things a little!
Code: Select all
while read line;do [ "${#line}" -gt "0" ] && echo "$line";done < text.txt | awk 'END{print NR}'
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
- MochiMoppel
- Posts: 2084
- Joined: Wed 26 Jan 2011, 09:06
- Location: Japan
Or go to Tools -> Word CountFlash wrote:The file will open in Geany. Then go to View -> Show line numbers and Geany numbers the lines.
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
- MochiMoppel
- Posts: 2084
- Joined: Wed 26 Jan 2011, 09:06
- Location: Japan
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..drunkjedi wrote:As I understand "grep -c ." prints only a count of matching lines which contains "."
Am I correct?
The letter "a" has no special meaning and therefore stands for ... well, the letter "a" .
Less funny but faster:musher0 wrote:For fun,
-- with awk:Code: Select all
awk 'NF > 0' text.txt | awk 'END{print NR}'
It's still slower than grep.awk '/./ {c++} END {print c}' text.txt