LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 06-09-2005, 12:42 AM   #1
tjgadu
Member
 
Registered: Jun 2005
Location: CyberIsland
Distribution: Debian and Fedora
Posts: 31

Rep: Reputation: 15
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????
 
Old 06-09-2005, 01:21 AM   #2
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Rep: Reputation: 15
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.
 
Old 06-09-2005, 02:54 AM   #3
tjgadu
Member
 
Registered: Jun 2005
Location: CyberIsland
Distribution: Debian and Fedora
Posts: 31

Original Poster
Rep: Reputation: 15
using regular expression

using regular expression, how can i output the number of blank lines found in a text file using the grep command
 
Old 06-09-2005, 04:40 AM   #4
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 45
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.
 
Old 06-09-2005, 04:49 AM   #5
Simon Bridge
Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 197Reputation: 197
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.
 
Old 06-09-2005, 04:51 AM   #6
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 45
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
 
Old 06-09-2005, 10:59 AM   #7
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Rep: Reputation: 15
thanx, slackie1000, i'll be more careful in the future
 
Old 06-09-2005, 04:01 PM   #8
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,535

Rep: Reputation: 148Reputation: 148
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading the number of Lines in a File Mistro116@yahoo.com Programming 31 11-24-2005 12:36 AM
count total number of lines in several files xushi Programming 5 11-12-2005 04:42 PM
How do i remove blank lines from a file? kakho Programming 1 04-15-2004 03:57 AM
Replace blank/almost blank lines in file Wynd Linux - General 3 01-27-2004 04:49 PM
cat: output specific number of lines mikeshn Linux - Software 3 12-31-2003 12:15 PM


All times are GMT -5. The time now is 07:16 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration