Home: Perl Programming Help: Regular Expressions:
Delete lines that not matches a pattern



edelcastillo
New User

Aug 22, 2006, 3:28 PM


Views: 6809
Delete lines that not matches a pattern

Friends:

I can't figure out how to do this... I have a variable "$var_search" with a text like this...

##eropiu opiuwqer oiwqureiou qopiuwer
##iuyasdf iouyasdf iuysadf
@@aoiuasdf oiausdf oiusadf
##asdf oiuqwer lkj˝lk a˝lkj
@@adsf˝jh kljhkljh iyqwer
&&asdfasd asdfasdf sadfadfa
%%asdfasdf asdfasdfa asdadsf
@@wqeoiruy yqwer qoyqwer
##sadff asdfads asdfasdf

I want to delete all lines that not have a @@ on it to finaly have:

@@aoiuasdf oiausdf oiusadf
@@adsf˝jh kljhkljh iyqwer
@@wqeoiruy yqwer qoyqwer

I can not figure out how to do it using s///g or tr///d. Thanks in advance for your help.

Erick.


davorg
Thaumaturge / Moderator

Aug 24, 2006, 6:28 AM


Views: 6794
Re: [edelcastillo] Delete lines that not matches a pattern

Something like this perhaps...


Code
$_ = <<'END'; 
##eropiu opiuwqer oiwqureiou qopiuwer
##iuyasdf iouyasdf iuysadf
@@aoiuasdf oiausdf oiusadf
##asdf oiuqwer lkj˝lk a˝lkj
@@adsf˝jh kljhkljh iyqwer
&&asdfasd asdfasdf sadfadfa
%%asdfasdf asdfasdfa asdadsf
@@wqeoiruy yqwer qoyqwer
##sadff asdfads asdfasdf
END

my $data = join "\n", /(\@\@.+)/g;

print $data;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks