LinuxQuestions.org
Review your favorite Linux distribution.
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 03-27-2008, 03:28 AM   #1
littletransformer
LQ Newbie
 
Registered: Nov 2007
Posts: 18

Rep: Reputation: 0
Question PERL:how to find a blank line in a file--regular expression


soursefile:
this is the first line
this is the second line

this is the third line
this is the fourth line

I want to tell wether there is a blank line and delete it.
so I write the code as follows:
#!/usr/bin/perl
$filename="d:\\text";
open($in,$filename)||die"can not open source file";
open(OUT,">>d:\\text01")||die"can not create file";
while($str=<$in>)
{
if($str!=~/\s/){print "yes";print OUT $str;}
}
close($in);
close(OUT);

but it didn't work.Do you have any ideas what to do?
 
Old 03-27-2008, 03:35 AM   #2
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Rep: Reputation: 15
if($str!=~/\s/){print "yes";print OUT $str;}
should be
unless($str =~ /^\s*$/){print "yes";print OUT $str;}

That'll match completely empty lines (^$) as well as line consisting of only whitespace.
 
Old 03-27-2008, 07:45 AM   #3
littletransformer
LQ Newbie
 
Registered: Nov 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Smile

thanks,it did work.but I am still confused about that,what shoud I do if I want to use the if sentense,since if and unless sentense can be represented for each other?
 
Old 03-27-2008, 06:35 PM   #4
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by littletransformer View Post
thanks,it did work.but I am still confused about that,what shoud I do if I want to use the if sentense,since if and unless sentense can be represented for each other?
Code:
if ($str !~ /^\s*$/){print "yes";print OUT $str;}

Last edited by angrybanana; 03-27-2008 at 06:37 PM.
 
Old 03-27-2008, 06:55 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Here's how I ignore/skip empty file recs and those starting with a comment in a cfg file
Code:
    # Process cfg file records
    while ( defined ( $cfg_rec = <CONFIG_FILE> ) )
    {
        # Remove unwanted chars
        chomp $cfg_rec;                 # newline
        $cfg_rec =~ s/#.*//;            # comments
        $cfg_rec =~ s/^\s+//;           # leading whitespace
        $cfg_rec =~ s/\s+$//;           # trailing whitespace

        next unless length($cfg_rec);   # anything left?
    
    # here you'd print the rec out to the new file if you get this far
 
  


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
regular expression with perl MissEileen Programming 3 03-11-2008 01:26 PM
Perl Regular Expression abdul_zu Linux - Software 2 08-20-2007 03:57 AM
Having trouble with a perl regular expression... jayemef Programming 3 08-25-2005 11:00 PM
Perl Regular Expression dilemma GATTACA Programming 1 03-27-2004 07:48 PM
Perl Regular Expression rch Programming 14 07-11-2003 11:00 PM


All times are GMT -5. The time now is 02:01 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