#!/bin/ksh
ed -s file <<-"EOF"
g/<MEMO>BALANCE/.-6,.+9d
1,$p
EOF
Although written and tested using the Korn shell, this will work with any shell that uses basic Bourne shell syntax. It will also work with ex instead of ed .
perl -ne 'push @b, $_; (@b==16) and $b[6]=~/<MEMO>BALANCE/?@b=():print shift @b; END{print @b if @b}' bomsom.file
However, the full script is more flexible since you could add to the work it can do; for example if you were to combine the requirement of this thread and the requirement of your other thread
Aia, I ran the Perl .pl file in Terminal and it works very well combining both requirements in one script like you said. I changed the file name of course, like this:
perl filter.pl cc3.ofx
However, when I added the variable to write the changes in the file (-i) the new file is missing the last 15 lines, and just those exact lines are printed in the terminal window. I ran the command like this:
The flag -i works only with the <> operator. Mostly, in throw away command line executions. I am using the <> in the script trying to be flexible from where it is getting the input (from stdin, and from file given). However, it is intended to show at stdout and thus it closes $ARGV (where <> is reading) and then it finishes the job.
If you are going to use it as such, a modification must be done. The highlighted part represents the modification. It has been tested, minimally. Please, try and let me know if you need something else.
#!/usr/bin/env perl
use strict;
use warnings;
my @buffer;
while(<>) {
push @buffer, $_;
if(@buffer == 16) {
if($buffer[6] =~ /<MEMO>BALANCE/) {
@buffer = ();
next;
}
print reverse_sign(shift @buffer);
}
if(eof) {
print reverse_sign(@buffer) if @buffer;
}
}
sub reverse_sign {
my $lines = \@_;
for my $line (@{$lines}) {
$line =~ s/^(<TRNAMT>)(-?\d+\.\d+)$/sprintf "%s%.2f", $1, -$2/e;
}
return @{$lines};
}