language-agnostic

Multiple pattern match and replace


How to extract patterns from a file and replace the multiple patterns with a new pattern from a file? For example: Lets say the pattern file is pattern.txt, as follows with 2,000 lines.

a  
b   
d  
e  
f  
....  
...  
...  

File to replace pattens is replace.txt containing:

a,1    
b,3  
c,5  
d,10  
e,14   
....  
...  
...   

The intended final file content for file patterns.txt is:

a,1    
b,3    
d,10  
e,14  
....  
...  
... 

Solution

  • Perl from command line,

    perl -i -pe'
      BEGIN{ local (@ARGV, $/, $^I) =pop; %h = split /[\s,]+/, <> }
      s| (\S+)\K |,$h{$1}|x
    ' pattern.txt replace.txt
    

    It slurps content of second file ($/ to undef), and temporarily disables in-place editing ($^I to undef), splits string on white-spaces/commas and populate %h hash in key/value manner. Then for every line of first file adds comma and value for current key.