Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-09-2005, 12:42 AM
|
#1
|
|
Member
Registered: Jun 2005
Location: CyberIsland
Distribution: Debian and Fedora
Posts: 31
Rep:
|
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????
|
|
|
|
06-09-2005, 01:21 AM
|
#2
|
|
Member
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110
Rep:
|
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"
Last edited by towlie; 06-09-2005 at 01:24 AM.
|
|
|
|
06-09-2005, 02:54 AM
|
#3
|
|
Member
Registered: Jun 2005
Location: CyberIsland
Distribution: Debian and Fedora
Posts: 31
Original Poster
Rep:
|
using regular expression
using regular expression, how can i output the number of blank lines found in a text file using the grep command
|
|
|
|
06-09-2005, 04:40 AM
|
#4
|
|
Senior Member
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037
Rep:
|
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!
Last edited by slackie1000; 06-09-2005 at 04:47 AM.
|
|
|
|
06-09-2005, 04:49 AM
|
#5
|
|
Guru
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,209
Rep: 
|
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.
Last edited by Simon Bridge; 06-09-2005 at 04:50 AM.
|
|
|
|
06-09-2005, 04:51 AM
|
#6
|
|
Senior Member
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037
Rep:
|
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
|
|
|
|
06-09-2005, 10:59 AM
|
#7
|
|
Member
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110
Rep:
|
thanx, slackie1000, i'll be more careful in the future
|
|
|
|
06-09-2005, 04:01 PM
|
#8
|
|
Moderator
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,470
Rep: 
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:57 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|