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? |
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. |
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?
|
Quote:
Code:
if ($str !~ /^\s*$/){print "yes";print OUT $str;} |
Here's how I ignore/skip empty file recs and those starting with a comment in a cfg file
Code:
# Process cfg file records |
All times are GMT -5. The time now is 02:57 AM. |