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

    I want the output in this way:

    JDW-HP 1MVWV93V7j2Hxsa0Ag6 24:00:00 SUBJECT_EXACT _Did you160serve our160country? -New VA Home.Loan Program_

    JDW-HP 1MVWV98Ddd4fLsa0Ag6 24:00:00 SUBJECT_EXACT Did.you.serve_our country? New-VA Home160Loan Program_

      Even easier. Same as above, but the regex simplifies to:

      s/<([0-9A-F]+)>/hex($1)/eg;
        stummala@sa-mua01:/home/stummala/scripts$./samp2 Can't locate feature.pm in @INC (@INC contains: /home/gsapan/perl5/lib +/perl5/sun4-solaris-thread-multi /home/gsapan/perl5/lib/perl5/x86_64- +linux-thread-multi /home/gsapan/perl5/lib/perl5 /usr/lib64/perl5/site +_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 +/usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl /usr/lib64/pe +rl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/vendor +_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64- +linux-thread-multi /usr/lib/perl5/5.8.8 .) at ./samp2 line 4. BEGIN failed--compilation aborted at ./samp2 line 4.