Regex multine replacement perl

2019-08-01 09:13发布

Posting this question after confusing people yesterday with a slightly sloppy prior version which didn't include sufficient information.

The questions is, I think, a fairly easy one but I am providing a LOT of context to help.

So, I have a file (FILEA.txt), created by an automated system a subset of that file is posted at the bottom of this question and headed as ===DATA===. I am not posting the entire file as it is 36k lines long....unless it is deemed necessary.

The file consists of MANY blocks of text, each of which describe entities (lines/circles/notations etc) on financial charts.

Each block starts with a consistent three lines...

CHARTS
  Color=14671839
  Layer=X

Where X is a number from 1 through 12. So in the example from the file shown below we have Layer=12.

I already have an automated process which runs daily to reformat the file into multiple formats (FILEB, FILEC, FILED etc) for users that need different formats.

That process consists of 6-7 files. 5-6 of them produce the various formats. These are batch files and one of the examples is below:

perl supplement.pl "s/Feed=5,8,F/Feed=3,8,F/g; s/@NQ#/NQ #F/g; s/@ES#/ES #F/g; s/@YM#/YM #F/g; s/QGC#/GC #F/g; s/QSI#/SI #F/g; s/QCL#/CL #F/g; s/QPA#/PA #F/g; s/QHG#/HG #F/g; s/QNG#/NG #F/g; s/@LB#/LBS #F/g; s/@O#/ZO #F/g; s/@RR#/ZR #F/g; s/@S#/ZS #F/g; s/@W#/ZW #F/g; s/@GF#/FC #F/g; s/@LE#/LC #F/g; s/QHO#/HO #F/g; s/@C#/ZC #F/g; s/@DX#/DX #F/g; s/AUDUSD.COMP/AUD A0-FX/g; s/USDCAD.COMP/CAD A0-FX/g; s/USDCHF.COMP/CHF A0-FX/g; s/EURUSD.COMP/EUR A0-FX/g; s/GBPUSD.COMP/GBP A0-FX/g; s/USDJPY.COMP/JPY A0-FX/g; s/NZDUSD.COMP/NZD A0-FX/g; s/@US#/ZB #F/g; s/@TY#/ZN #F/g;" c:/PATHTOFILE/FILEA.txt c:/PATHTOFILE/FILEB.txt

It reads in FILEA.txt, performs the conversions and outputs to FILEB.txt

The final file is a supplement.pl, which in addition to the format changes that are contained within each of the batch files, strips two elements from each and every format. The two lines in supplement.pl that strips out alert lines and circles each start with "$contents =~".

I am looking to add a third 'strip' line that strips out each block in its entirety WHERE Layer=7. I figured this would look for Charts/n Color=14671839/n Layer=7 all the way through to the next "Charts" and replace the lot with "Charts" meaning the prior block vanishes. I hope this makes sense.

I have tried to be comprehensive here but I am happy to provde additional information if it helps.

===START SUPPLEMENT.PL===

#!/usr/bin/perl

$sub = shift @ARGV;
$input = shift @ARGV;
$output = shift @ARGV;


open INPUT, "<","$input" or die $!;         
open OUTPUT, ">","$output" or die $!;           

local $/ = undef;
$contents = <INPUT>;

@subs = split('; ', $sub);
foreach (@subs)
{
    @re = split( m/(?<!\\)\//, $_);
    tr/\\//d for @re;

    $contents =~ s/$re[1]/$re[2]/g;
}

$contents =~ s/\[19\]\= <<<\s+Circle(?s).*?\[28\]\=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//g;
$contents =~ s/\[19\]\= <<<\s+Draw Line Alarmed(?s).*?\[28\]\=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//g;
===NEED STRIP NEW LINE HERE===

print OUTPUT $contents;

close INPUT;
close OUTPUT;

===END SUPPLEMENT.PL===

==START SAMPLE DATA===

CHARTS
  Color=14671839
  Layer=12
  [0] Font=MS SAN SERIF,10,0,F,F,
  [1] Show=F,F,F,T,T,F,T,F,F,T,
  [2] Scale=F,F,T,F,F,F,F,F,0,T,F,
  [3] Grid=F,F,F,F,1,
  [4] Paint=F,F,F,0,0,0,
  [5] Bars=1,2000,1970,
  [6] Colors=65793,0,0,8388608,32767,65793,0,0,
  [7] Theme=6,0,255,65280,7566080,0,16777215,0,
  [8] Candles=F,F,F,F,1,F,
  [9] Filter=T,F,F,F,F,0,0
  [10] Type=F,F,F,F,15,44656,
  [11] Spacing=3,49,0,10,4,-7,73,
  [12] Range=56.16,18.72,0.01,0.01
  [13] Volume=0,100,0,0,0,0,
  [14] Refresh=0,0,0,
  [15] Feed=5,1,F,
  [16] Template=
  [17] Symbol=XLP.W
  [18] SubSizes=0,0,0,0,0,0,0,0,0,0,

  [19]= <<<    Note: SPDR Consumer Staples Sector   >>>
  [20]=76,44700,0,0,513,2304,29325,4158,10,59619,29671,10589,21907,2496,29325,4158,10,
  [21]=-2083405824,1,0,8388608,255,16711680,16711680,16711680,16711680,11842740,0,0,0,0,0,0,32,32,32,32,32,32,32,32,
  [22]=-2083405705,1,1035611788,-1096111445,1151639552,1101497088,1147076608,1435707648,1696464,5525461,1921845696,659518,13,29,182101436,1696464,1696712,1696552,139104368,4830724,1696712,2,1696712,4225200,139104368,1696552,
  [23]=8,0,2,67167744,1150116,0,1150116,67137536,1150828,0,0,0,0,0,0,1,1,1,1,3,1,0,0,0,0,
  [24]=0,71,1,0,0,0,0,0,0,0,0,0,0,84,0,0,0,0,0,0,0,0,28,83,80,68,82,32,67,111,110,115,117,109,101,114,32,83,116,97,112,108,101,115,32,83,101,99,116,111,114,
  [25]=255,242,118,160,226,25,0,80,120,35,2,0,0,0,0,71,1,0,0,0,0,0,0,0,0,0,0,213,15,122,1,240,27,244,118,0,0,0,0,252,36,11,116,152,13,232,115,116,20,35,
  [26]=0,71,1,0,0,236,162,218,10,0,0,0,0,192,73,156,1,177,2,0,0,0,0,0,0,24,198,216,10,28,0,0,0,166,13,232,115,42,0,0,0,16,227,25,0,92,15,167,118,36,
  [27]=163,218,10,252,197,216,10,28,0,0,0,0,0,0,0,224,228,25,0,228,4,0,0,105,15,167,118,110,115,117,109,0,0,0,0,28,0,0,0,0,0,0,64,1,0,0,0,68,227,25,
  [28]=5506571,1696712,139104368,139104368,5506571,1696712,139104368,139104368,1944577216,24776661,0,327,0,0,3366912,1,1435707981,-2147483648,1995709424,327,0,0,0,2298996,0,

  [19]= <<<    Line     h    19.28    58.58   >>>
  [20]=65,44701,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [21]=16711680,16711680,8421376,0,255,255,16711680,16711680,65280,16744447,32767,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [22]=0,0,118.33334,0,118.33334,19.2794,58.58068,58.58068,5,0,0,0,0,0,0,1,0,0.1,0,0,0,0,0,0,0,0,
  [23]=1,0,0,0,1161209,0,1161209,255,1090313,1,2,2,1,0,1,1,1,1,1,1,1,0,0,0,0,
  [24]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [25]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [26]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [27]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [28]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

  [19]= <<<    Line     i    45.35    84.64   >>>
  [20]=65,44702,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [21]=16711680,16711680,8421376,0,255,255,16711680,16711680,65280,16744447,32767,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [22]=0,0,359.66666,0.33333,359.66666,45.34758,84.64344,84.64344,5,0,0,0,0,0,0,1,0,0.1,0,0,0,0,0,0,0,0,
  [23]=1,0,0,0,1161209,0,1161209,255,1131025,1,2,2,1,0,1,1,1,1,1,1,1,0,0,0,0,
  [24]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [25]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [26]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [27]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [28]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

CHARTS
  Color=14671839
  Layer=12
  [0] Font=MS SAN SERIF,10,0,F,F,
  [1] Show=F,F,F,T,T,F,T,F,F,T,
  [2] Scale=F,F,T,F,F,F,F,F,0,T,F,
  [3] Grid=F,F,F,F,1,
  [4] Paint=F,F,F,0,0,0,
  [5] Bars=1,2000,1970,
  [6] Colors=65793,0,0,8388608,32767,65793,0,0,
  [7] Theme=6,0,255,65280,7566080,0,16777215,0,
  [8] Candles=F,F,F,F,1,F,
  [9] Filter=T,F,F,F,F,0,0
  [10] Type=F,F,F,F,15,44657,
  [11] Spacing=10,261,0,10,4,-7,73,
  [12] Range=53.46,41.04,0.01,0.01
  [13] Volume=0,100,0,0,0,0,
  [14] Refresh=0,0,0,
  [15] Feed=5,1,F,
  [16] Template=
  [17] Symbol=EWW.W
  [18] SubSizes=0,0,0,0,0,0,0,0,0,0,

  [19]= <<<    Line     p    57.74    31.41   >>>
  [20]=65,47387,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [21]=16711680,16711680,8421376,0,255,255,16711680,16711680,65280,16744447,32767,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [22]=0,0,82.25,0,82.25,57.74,31.41068,31.41068,5,0,0,0,0,0,0,1,0,0.1,0,0,0,0,0,0,0,0,
  [23]=1,0,0,0,1161209,0,1161209,255,1130628,1,2,2,1,0,1,1,1,1,1,1,1,0,0,0,0,
  [24]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [25]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [26]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [27]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [28]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

  [19]= <<<    Line     q    77.54    51.23   >>>
  [20]=65,47388,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [21]=16711680,16711680,8421376,0,255,255,16711680,16711680,65280,16744447,32767,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [22]=0,0,65.5,0.25,65.5,77.53828,51.22672,51.22672,5,0,0,0,0,0,0,1,0,0.1,0,0,0,0,0,0,0,0,
  [23]=1,0,0,0,1161209,0,1161209,255,1130301,1,2,2,1,0,1,1,1,1,1,1,1,0,0,0,0,
  [24]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [25]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [26]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [27]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [28]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

  [19]= <<<    Line     r    62.76    36.45   >>>
  [20]=65,47389,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [21]=16711680,16711680,8421376,0,255,255,16711680,16711680,65280,16744447,32767,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [22]=0,0,165.25,0,165.25,62.75862,36.44707,36.44707,5,0,0,0,0,0,0,1,0,0.1,0,0,0,0,0,0,0,0,
  [23]=1,0,0,0,1161209,0,1161209,255,1150130,1,2,2,1,0,1,1,1,1,1,1,1,0,0,0,0,
  [24]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [25]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [26]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [27]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [28]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

CHARTS
  Color=14671839
  Layer=12
  [0] Font=MS SAN SERIF,10,0,F,F,
  [1] Show=F,F,F,T,T,F,T,F,F,T,
  [2] Scale=F,F,T,F,F,F,F,F,0,T,F,
  [3] Grid=F,F,F,F,1,
  [4] Paint=F,F,F,0,0,0,
  [5] Bars=1,2000,1970,
  [6] Colors=65793,20480,1376384,8388608,32767,65793,0,0,
  [7] Theme=6,0,255,65280,7566080,0,16777215,0,
  [8] Candles=F,F,F,F,1,F,
  [9] Filter=T,F,F,F,F,0,0
  [10] Type=F,F,F,F,15,44658,
  [11] Spacing=3,290,0,10,4,-7,73,
  [12] Range=39.6,7.92,0.62,0.01
  [13] Volume=0,100,0,0,0,0,
  [14] Refresh=0,0,0,
  [15] Feed=5,1,F,
  [16] Template=
  [17] Symbol=XHB.W
  [18] SubSizes=0,0,0,0,0,0,0,0,0,0,

  [19]= <<<    Line     o    19.94    43.15   >>>
  [20]=65,9114,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [21]=16711680,16711680,8421376,0,255,255,16711680,16711680,65280,16744447,32767,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [22]=0,0,27.5,0,27.5,19.94,43.1505,43.1505,5,0,0,0,0,0,0,1,0,0.1,0,0,0,0,0,0,0,0,
  [23]=1,0,0,0,1160101,0,1160101,255,1100423,1,2,2,1,0,1,1,1,1,1,1,1,0,0,0,0,
  [24]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [25]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [26]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [27]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [28]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

  [19]= <<<    Line     p     7.96    31.18   >>>
  [20]=65,9115,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [21]=16711680,16711680,8421376,0,255,255,16711680,16711680,65280,16744447,32767,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [22]=0,0,-0.25,0.25,-0.25,7.95896,31.18213,31.18213,5,0,0,0,0,0,0,1,0,0.1,0,0,0,0,0,0,0,0,
  [23]=3,0,0,0,1150605,0,1150605,255,1090313,1,2,2,1,0,1,1,1,1,1,1,1,0,0,0,0,
  [24]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [25]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [26]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [27]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [28]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

  [19]= <<<    Line     q    11.20    34.43   >>>
  [20]=65,9116,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [21]=16711680,16711680,8421376,0,255,255,16711680,16711680,65280,16744447,32767,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [22]=0,0,88.75,0.25,88.75,11.20242,34.42559,34.42559,5,0,0,0,0,0,0,1,0,0.1,0,0,0,0,0,0,0,0,
  [23]=3,0,0,0,1160101,0,1160101,255,1110624,1,2,2,1,0,1,1,1,1,1,1,1,0,0,0,0,
  [24]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [25]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [26]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [27]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [28]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

  [19]= <<<    Line     r    18.50    41.72   >>>
  [20]=65,9117,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [21]=16711680,16711680,8421376,0,255,255,16711680,16711680,65280,16744447,32767,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [22]=0,0,133,0.5,133,18.5002,41.72338,41.72338,5,0,0,0,0,0,0,1,0,0.1,0,0,0,0,0,0,0,0,
  [23]=3,0,0,0,1160101,0,1160101,255,1120427,1,2,2,1,0,1,1,1,1,1,1,1,0,0,0,0,
  [24]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [25]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [26]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [27]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  [28]=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

  [19]= <<<    Note: SPDR Homebuilders ETF   >>>
  [20]=76,44659,0,0,513,63488,25,5552,29674,39301,9851,27405,26412,63420,25,2963,29325,
  [21]=-2083405824,1,0,8388608,255,16711680,16711680,16711680,16711680,11842740,0,0,0,0,0,0,32,32,32,32,32,32,32,32,
  [22]=-2083405705,1,1056964608,1106597205,1155792896,1090936302,1146437632,0,1,0,0,112,-1,-1,1944577820,1944579083,0,0,0,0,0,1921845696,0,31918584,0,16,
  [23]=

==END SAMPLE DATA===

1条回答
仙女界的扛把子
2楼-- · 2019-08-01 09:29

So, from your question:

I am looking to add a third 'strip' line that strips out each block in its entirety WHERE Layer=7.

Did you noticed, that in your sample data isn't any section containing Layer=7?

Now the solution:

Excetly your code, with the added line from the comments (e.g. from the answers to your previous same question.

#!/usr/bin/perl

$sub = shift @ARGV;
$input = shift @ARGV;
$output = shift @ARGV;


open INPUT, "<","$input" or die $!;         
open OUTPUT, ">","$output" or die $!;           

local $/ = undef;
$contents = <INPUT>;

@subs = split('; ', $sub);
foreach (@subs)
{
    @re = split( m/(?<!\\)\//, $_);
    tr/\\//d for @re;

    $contents =~ s/$re[1]/$re[2]/g;
}

$contents =~ s/\[19\]\= <<<\s+Circle(?s).*?\[28\]\=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//g;
$contents =~ s/\[19\]\= <<<\s+Draw Line Alarmed(?s).*?\[28\]\=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,//g;

#this is the added line
$contents = join '', grep { !/Layer\s*=\s*7\b/ } split /^(?=CHARTS)/m, $contents;
#or this works too
#$contents = join 'CHARTS', grep { !/Layer\s*=\s*7\b/ } split /\bCHARTS\b/, $contents;

print OUTPUT $contents;

close INPUT;
close OUTPUT;

The above:

  • Is tested against your data.
  • It produces output.
  • And, if you will have in the input data some CHARTS section containing Layer=7 it will be removed.
查看更多
登录 后发表回答