0

I'd like to remove a specific range of element form my array.
I have a txt file:

Rbody ...
333
444
555
666
END

Shell ...
Node ...

Rbody ...
333
444
555
666
END

What I would like to do is to remove the Elements starting with the word "Rbody" until the word "END" from my file.

#while (<INC>) {
#   if (!/^RBODY/)
#   {
#       push(@alllines,$_);
#   }
#}

only removes the lines with the word Rbody...I guess I need some kinda loop. :/

Thx for ur help.

Christopher Neylan
  • 8,018
  • 3
  • 38
  • 51
  • Are each of those seperate lines in the txt file? Also do you have to save these somehow or do you just have to make a text file without those entries? Also, is Rbody ... 333 444 555 666 END one line, and you have to remove that line? – KateMak Mar 20 '13 at 15:25
  • @squiguy: No, it is not one line. Rbody is lets say the first and END the 10th..inbetween some data. The entire block should be removed, the rest of the data should be pushed in my array respectively. – Hauke Stricker Mar 21 '13 at 07:01

4 Answers4

3

It's a little esoteric, but this is a good use case for the "flip-flop" operator:

while (<INC>) {
    push @alllines, $_ unless /^Rbody/ .. /^END/;
}

The expression /^Rbody/ .. /^END/ returns false until $_ matches the regular expression /^Rbody/. Then it returns true until it matches /^END/.

mob
  • 117,087
  • 18
  • 149
  • 283
1

Try using two loops. The outer loop reads the lines as normal. If it finds a line like /^Rbody/i, then it will use an inner loop to skip over everything until it sees "END":

while( defined(my $line = <INC>) ){
   chomp( $line );
   if( $line =~ /^Rbody/i ){
       while( defined($line = <INC>) ){
           chomp( $line );
           last if $line eq "END";
       }
   } else {
       push( @alllines, $line );
   }
}
Christopher Neylan
  • 8,018
  • 3
  • 38
  • 51
0

Just use a state variable that keeps track of whether the current line should be printed.

use strict;
use warnings;

my $print = 1;

while (<DATA>) {
  if ($print) {
    $print = 0 if /^Rbody/i;
    print if $print;
  }
  elsif (/^END$/) {
    $print = 1;
  }
}
Borodin
  • 126,100
  • 9
  • 70
  • 144
0

Unfortunately no version seems to work:

The first version prints every single line, the other two provide no output.

My text looks like this:

RBODY /
sdsd
sdsdsd
sdsd
        END

SHELL /  9171781 9001914 9073136 9073137 9073118 9073115                         
SHELL /  9171782 9001914 9073117 9073120 9073119 9073116                         
SHELL /  9171783 9001914 9073118 9073121 9073120 9073117                         
SHELL /  9171784 9001914 9073137 9073138 9073121 9073118                         
SHELL /  9171785 9001914 9073120 9073123 9073122 9073119                         
SHELL /  9171786 9001914 9073121 9073124 9073123 9073120                         
# [SDM:HISTORY:VERSION] = "History_Variante_DGS"
#
# S01__B70D_revo: *Autor: revo
#                 *Datum: 11.12.12
#                 *Basis: -------
#                 - automatischer Positionierungsprozess
#                 - verwendetes Dummysessionfile: Sessiondummy____au481_______LFaH3_sitzpos_S01__B70D_revo.ses
#                 - verwendetes Gurtsessionfile : Sessiongurt_____au481_______LFaH3_sitzpos_S01__Basi_revo.ses
#                 - verwendetes Sitzsessionfile : Sessionsitz_____au481__0____LFaH3_sitzpos_S01__B001_revo_k14m.ses
#
#

The code up until now:

#!/usr/bin/perl
#
#print "Bitte geben sie das Dummy Include an:\n";
#chop($dummyfile = <>);
#print "\n";
#print "Bitte geben sie das Basis Include an:\n";
#chop($basisfile = <>);
#print "\n";

$dummyfile = 'Dummy_mu5070b___mvs2g__1_cb21_k19_m04F_bsd_w3_bfrei__krmi_040_.inc';
$newinc = "dummy_include.inc";
open(INC, $dummyfile);
open(NEWINC, ">$newinc");


.... missing regex


print @alllines;