in reply to How do i replace one variable with another after searching for a pattern
Like this?
#!/usr/bin/perl use strict; use warnings; use feature qw/say/; while(<DATA>) { chomp; s/<([0-9A-F]+)>/"<" . hex($1) . ">"/eg; say; } __DATA__ JDW-HP 1MVWV93V7j2Hxsa0Ag6 24:00:00 SUBJECT_EXACT _Did you<A0>serve ou +r<A0>country? -New VA Home.Loan Program_ JDW-HP 1MVWV98Ddd4fLsa0Ag6 24:00:00 SUBJECT_EXACT Did.you.serve_our co +untry? New-VA Home<A0>Loan Program_
Output:
$ perl 1095788.pl JDW-HP 1MVWV93V7j2Hxsa0Ag6 24:00:00 SUBJECT_EXACT _Did you<160>serve o +ur<160>country? -New VA Home.Loan Program_ JDW-HP 1MVWV98Ddd4fLsa0Ag6 24:00:00 SUBJECT_EXACT Did.you.serve_our co +untry? New-VA Home<160>Loan Program_ $
For more on the s// (substitution) operator, see e.g. Regexp Quote Like Operators. The above substitution is fairly straightforward; it looks for angle brackets with one of more hex digits in them, and replaces them with the result of a snippet of code that gets evaluated for each match (hence the /e modifier to the regex). The /g modifier makes this a global operation so that all occurrences are given this treatment, not just the first.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How do i replace one variable with another after searching for a pattern
by Niklaus Mikaelson (Initiate) on Jul 31, 2014 at 18:25 UTC | |
by AppleFritter (Vicar) on Jul 31, 2014 at 18:27 UTC | |
by Niklaus Mikaelson (Initiate) on Jul 31, 2014 at 18:32 UTC | |
by AppleFritter (Vicar) on Jul 31, 2014 at 18:39 UTC |