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???? |
Using grep and wc in a bash script (make sure you watch
your whitespace around variables): Code:
#!/bin/bash run with: "numblanks testfile" |
using regular expression
using regular expression, how can i output the number of blank lines found in a text file using the grep command
|
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 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! |
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. |
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 slackie1000 |
thanx, slackie1000, i'll be more careful in the future
|
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. |