G'day LinuxMatt,

Here's the basis of how I might approach this:

$ perl -Mwarnings -Mstrict -E ' my @test_data = ( "comment follows: #qwerty", " whitespace at start", "whitespace at end ", "Total: some total", ", starts with a comma", ); my @all_patterns = (q{#.*}, q{^\s+}, q{\s+$}, q{^Total}, q{^,}); my @files = qw{file1 file2 file3}; my %file_filters = ( file1 => [0, 2, 4], file2 => [2, 1, 3], file3 => [4, 3, 2] ); for my $file (@files) { say "File: $file Patterns: @all_patterns[@{$file_filters{$fil +e}}]"; my @this_files_data = @test_data; for my $line (@this_files_data) { say "Start: |$line|"; for (@{$file_filters{$file}}) { $line =~ s/$all_patterns[$_]//; } say "End: |$line|"; } } ' File: file1 Patterns: #.* \s+$ ^, Start: |comment follows: #qwerty| End: |comment follows:| Start: | whitespace at start| End: | whitespace at start| Start: |whitespace at end | End: |whitespace at end| Start: |Total: some total| End: |Total: some total| Start: |, starts with a comma| End: | starts with a comma| File: file2 Patterns: \s+$ ^\s+ ^Total Start: |comment follows: #qwerty| End: |comment follows: #qwerty| Start: | whitespace at start| End: |whitespace at start| Start: |whitespace at end | End: |whitespace at end| Start: |Total: some total| End: |: some total| Start: |, starts with a comma| End: |, starts with a comma| File: file3 Patterns: ^, ^Total \s+$ Start: |comment follows: #qwerty| End: |comment follows: #qwerty| Start: | whitespace at start| End: | whitespace at start| Start: |whitespace at end | End: |whitespace at end| Start: |Total: some total| End: |: some total| Start: |, starts with a comma| End: | starts with a comma|

Be aware how the order of the patterns matters. At the start of the output, you'll see:

File: file1 Patterns: #.* \s+$ ^, Start: |comment follows: #qwerty| End: |comment follows:|

However, had those first two patterns been reversed, you'd see:

File: file1 Patterns: \s+$ #.* ^, Start: |comment follows: #qwerty| End: |comment follows: |

Note the extra space after "comment follows:".

-- Ken


In reply to Re: Filtering files with lists of substitution patterns by kcott
in thread Filtering files with lists of substitution patterns by LinuxMatt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.