royalreddy has asked for the wisdom of the Perl Monks concerning the following question:

Hi, how can I delete block of data based on some perticular string, string is some where in the block ( not in first or last) here i wanted to delete block1, because it has remove in it file.txt block1 start hehu7 gjwsdh hsjdhj remove hjhsj jdkwj hwjdhjwh end block2 start crgr hjdhwjd wjdkwj wkjw g hjhd end
  • Comment on how to remove block of data from file based on string

Replies are listed 'Best First'.
Re: how to remove block of data from file based on string
by Xiong (Hermit) on Mar 14, 2012 at 23:22 UTC

    Sorry; I can't answer your question because I can't understand it. Let me try to show you how I think you might ask.


    I have the following input data:

    Input:
    block1 start hehu7 gjwsdh hsjdhj remove hjhsj jdkwj hwjdhjwh end block2 start crgr hjdhwjd wjdkwj wkjw g hjhd end

    I want to remove the string that starts with 'start', includes 'remove', and ends with 'end':

    Output:
    block1 block2 start crgr hjdhwjd wjdkwj wkjw g hjhd end

    Of course this may not be what you want at all. Why not try to be a bit more explicit and we'll see what we can do to help?

    Death only closes a Man's Reputation, and determines it as good or bad. —Joseph Addison
      ya, this is what I want.......
        Here is your solution:
        my $str = <<'EOB'; start hehu7 gjwsdh hsjdhj remove hjhsj jdkwj hwjdhjwh end start crgr hjdhwjd wjdkwj wkjw g hjhd end EOB while ($str =~ /(\bstart\b.*?\bend\b\s*)/gs) { my $block = $1; if ($block =~ /\bremove\b/) { $str =~ s/\Q$block\E//; } } print $str; __END__ start crgr hjdhwjd wjdkwj wkjw g hjhd end
        A reply falls below the community's threshold of quality. You may see it by logging in.