Reputation: 83
I am new to perl and have no previous programming experience with any other programming language. I am trying to delete/skip few line using a do-while loop. I am trying to delete/skip data between two tags: <worker>
and </workers>
.
Code:
if($work=/^<worker>/)
{
do
{
delete $work[$i];
++$i;
}
print $work,"\n";
}
Upvotes: 1
Views: 1394
Reputation: 69314
while (<>) {
print unless m|<worker>| .. m|</workers>|;
}
Seems slightly strange to start and end on different tags. Should they both be 'worker' or 'workers'?
Upvotes: 1
Reputation: 2710
I assume you have data like:
my $x = <<END;
Text to keep
<worker> text to be deleted </worker>
Text to keep again
<worker>
Text to be deleted
</worker>
END
If you want to remove <worker></worker>
tags as well:
$x =~ s/<worker>.*?<\/worker>//g;
If you need to keep tags:
$x =~ s/<worker>.*?<\/worker>/<worker><\/worker>/g;
However, this will work only if you can guarantee <worker>
tags are not nested. Following string will cause error:
<worker> lalala <worker> bababa </worker> lalala </worker>
To do this work really good and safe you need to use parsing modules like HTML::TreeBuilder
or XML::Twig
Upvotes: 0
Reputation: 2247
I am assuming you want to skip data between tags <workers>
and </workers>
. You need to use .. in regular expression
This is a one liner that does it
perl -ne 'print unless /<workers>/../<\/workers>/' <file_name>
If you want to do it in the script, add this line after reading a line from the file
next if /<worker>/../<\/workers>/;
You can start perl with this very good presentation by brian d foy
Upvotes: 1