LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-14-2006, 01:55 AM   #1
Garda
Member
 
Registered: May 2005
Distribution: Debian
Posts: 83
Blog Entries: 2

Rep: Reputation: 15
Perl: testing for blank lines


i'm trying to find blank lines in a text file
this is the code

Code:
use strict;
use warnings;

open (FILE, '<', 'file1.txt') or die "$!";

while (<FILE>)
{
	if ($_ == "")
	{
		print "it's blank\n";
	}
	elsif ($_ != "")
	{
		print "it's NOT blank\n";
	}
}
this is file1.txt

Code:
1

2

3

4
it seems to work ok, but i keep getting warnings

i'm pretty sure that
if ($_ == "")
is the wrong way to test for an empty string, what should i be doing instead?

edit: i'm kinda confused also, because i think blank lines are actually nothing, followed by the new like character, but i still get the "it's blank" message

Last edited by Garda; 11-14-2006 at 01:58 AM.
 
Old 11-14-2006, 02:10 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Try this:
Code:
#!/bin/perl
use strict;
use warnings;

open (FILE, '<', 'file1.txt') or die "$!";

while (<FILE>)
{
  if ($_ =~ /^$/)
  {
    print "it's blank\n";
  }
  else
  {
    print "it's NOT blank\n";  
  }
}
This: $_ =~ /^$/ can also be written as /^$/. Perl assumes a check against $_ in the last case.

Hope this helps.
 
Old 11-14-2006, 02:20 AM   #3
Garda
Member
 
Registered: May 2005
Distribution: Debian
Posts: 83
Blog Entries: 2

Original Poster
Rep: Reputation: 15
thanks for that, i was pretty sure that i was checking for empty lines in the wrong way
 
Old 11-16-2006, 07:26 PM   #4
BCarey
Senior Member
 
Registered: Oct 2005
Location: New Mexico
Distribution: Slackware
Posts: 1,476

Rep: Reputation: Disabled
You might want to try:

Code:
/^\s*$/
if your "blank" lines may contain spaces or tabs.

Brian
 
Old 11-16-2006, 07:39 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
You used the == to test for an empty string, but this is the numerical equality test. You should use eq for strings. Also $_ will not equal "" for a blank line - it will equal "\n". If you want to get rid of the newline character before testing, use chomp first.
Code:
while (<FILE>) {
    chomp;
    if ( $_ eq "" ) { print "empty line at input line number $.\n"; }
}
A regular expression might also be useful, matching not only completely empty lines but those with whitespace. You don't have to chomp if you're using regular expressions - they're smart enough to handle them. Personally I like to chomp anyway - I find that more often than not I don't want the \n on the end of the line, plus I like the word "chomp":
Code:
while (<FILE>) {
    chomp;   # just because!
    if ( /^\s*$/ ) { 
        print "empty line, or line with only whitespace at line $.\n"; 
    }
}
P.S. don't forget to close your FILE handle.
P.P.S. when calling open, you don't need to separate the <. You can call it like this: open(FILE, "<myfilename") || die ...;

Last edited by matthewg42; 11-16-2006 at 07:40 PM. Reason: silly typo corrction
 
  


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
Remove Blank Lines wwnexc Linux - Software 2 05-06-2006 11:14 AM
output number of blank lines tjgadu Linux - Newbie 7 06-09-2005 04:01 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
Screen goes blank(random) to vertical lines bruce1271 Linux - General 2 11-15-2003 11:02 AM


All times are GMT -5. The time now is 10:56 AM.

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