LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   output number of blank lines (https://www.linuxquestions.org/questions/linux-newbie-8/output-number-of-blank-lines-331749/)

tjgadu 06-09-2005 12:42 AM

output number of blank lines
 
Suppose I have the following text in a text file named myfile:

THis is the first line



THis is the 2nd one

this is the third line


Using the grep command, how can i count the number fo blank lines????

towlie 06-09-2005 01:21 AM

Using grep and wc in a bash script (make sure you watch
your whitespace around variables):
Code:

#!/bin/bash

declare -i total
declare -i nonblank
declare -i blank

nonblank=`grep . $1 | wc --lines`
total=`cat $1 | wc --lines`

blank=$total-$nonblank
echo "$blank blank lines in $1"

EDIT: forgot, save code in bash script 'numblanks' and
run with: "numblanks testfile"

tjgadu 06-09-2005 02:54 AM

using regular expression
 
using regular expression, how can i output the number of blank lines found in a text file using the grep command

slackie1000 06-09-2005 04:40 AM

hi there,
are you sure this is not your home work? if it is not i am sorry.
but if it is .... bad!
your solution
Code:

grep -c '^$' $YOURFILE
regards
slackie1000

[edit] man, terrible mistake.. you double posted in the same forum your homework! shame on you.
be less lazy and make it by yourself. i will just leave the solution here cause i think it is better than the other one provided in the other post. shame on you.. reported!

Simon Bridge 06-09-2005 04:49 AM

You can use sed to count lines too - "man sed".

If its easier to count [i]all[i] the lines in the file - you can use nl to count the non-blank lines and subtract.

More generally: in the "My LQ" box to the right, you see a link called: Edit Profile. Please use that to add your distro(s) and some sort of location. It helps you get better replies.

When you want to post a question, try sticking the question (maybe a short form) into the subject line. This way people know before they click on it, what you are asking. If this case, you could have put "how do I count blank lines using grep?" That would have fit. Better is to leave it open in case yu're barking up the wrong tree: something like "How do I count blank lines in txt file?" (This approach also makes you look smarter.)

If you go to the tutorials section, there are very helpful tutorials about how to post messages so you get the best answers.

slackie1000 06-09-2005 04:51 AM

hi there,
towlie, this is double post.. typical homework question...
i will post the solution and report this post ..
you don't need wc ..
Code:

grep -c '^$' $YOURFILE
regards
slackie1000

towlie 06-09-2005 10:59 AM

thanx, slackie1000, i'll be more careful in the future

Mara 06-09-2005 04:01 PM

tjgadu, your two threads were merged.

Please don't post homework questions. If you do, show us that you tried to do it yourself and have problems with details: manual page is not clear, you have written a script and it doesn't work etc.


All times are GMT -5. The time now is 08:32 AM.