in reply to Filtering files with lists of substitution patterns

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