Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. ~/naive-file.txt
  2. ~/old-text.txt
  3. ~/new-text.txt
  4.  
  5. $ cat ~/naive-file.txt
  6. Sed id ligula quis est convallis tempor.
  7.  
  8. This is the old text.
  9.  
  10. It might have multiple lines and some special characters like these { & % #)!
  11. etc...
  12.  
  13.  
  14. Nunc aliquet, augue nec adipiscing interdum, lacus tellus malesuada massa, quis
  15. varius mi purus non odio.
  16.  
  17. $ cat ~/old-text.txt
  18. This is the old text.
  19.  
  20. It might have multiple lines and some special characters like these { & % #)!
  21. etc...
  22.  
  23. $ cat ~/new-text.txt
  24. This is the new text.
  25.  
  26. It could also have multiple lines and special characters like these { & %
  27. etc...
  28.  
  29. Sed id ligula quis est convallis tempor.
  30.  
  31. This is the new text.
  32.  
  33. It could also have multiple lines and special characters like these { & %
  34. etc...
  35.  
  36.  
  37. Nunc aliquet, augue nec adipiscing interdum, lacus tellus malesuada massa, quis
  38. varius mi purus non odio.
  39.  
  40. #!/usr/bin/perl
  41. use warnings;
  42. use strict;
  43.  
  44. open my $ot, '<', 'old-text.txt' or die $!;
  45. chomp( my @lines = <$ot> );
  46. open my $nt, '<', 'new-text.txt' or die $!;
  47. my %replace;
  48. @replace{@lines} = <$nt>;
  49. chomp for values %replace;
  50.  
  51. my $regex = join '|', map quotemeta, @lines;
  52. open my $in, 'naive-file.txt' or die $!;
  53. while (<$in>) {
  54. s{(.*)}{$replace{$1} // $1}e;
  55. print;
  56. }
  57.  
  58. my $regex = join '|', map quotemeta, sort { length $b <=> length $a } @lines;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement