Welcome to the most active Linux Forum on the web.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org PERL:how to find a blank line in a file--regular expression
 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

 03-27-2008, 03:28 AM #1 littletransformer LQ Newbie   Registered: Nov 2007 Posts: 18 Rep: 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?
 03-27-2008, 03:35 AM #2 exscape Member   Registered: Aug 2007 Location: Sweden Distribution: OS X, Gentoo, FreeBSD Posts: 82 Rep: 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.
 03-27-2008, 07:45 AM #3 littletransformer LQ Newbie   Registered: Nov 2007 Posts: 18 Original Poster Rep: 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?
03-27-2008, 06:35 PM   #4
angrybanana
Member

Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep:
Quote:
 Originally Posted by littletransformer 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.  03-27-2008, 06:55 PM #5 chrism01 Guru Registered: Aug 2004 Location: Sydney Distribution: Centos 6.6, Centos 5.10 Posts: 16,453 Rep: 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 = ) ) { # 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

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post MissEileen Programming 3 03-11-2008 01:26 PM abdul_zu Linux - Software 2 08-20-2007 03:57 AM jayemef Programming 3 08-25-2005 11:00 PM GATTACA Programming 1 03-27-2004 07:48 PM rch Programming 14 07-11-2003 11:00 PM

All times are GMT -5. The time now is 08:13 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -