Niklaus Mikaelson has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I would like to replace the hexadecimal value that is in the angular brackets with the decimal value of the same and print the lines with decimal value in it.

JDW-HP 1MVWV93V7j2Hxsa0Ag6 24:00:00 SUBJECT_EXACT _Did you<A0>serve our<A0>country? -New VA Home.Loan Program_

JDW-HP 1MVWV98Ddd4fLsa0Ag6 24:00:00 SUBJECT_EXACT Did.you.serve_our country? New-VA Home<A0>Loan Program_

This is the code i have written, how do i replace the hexadecimal value with the decimal value and print the whole line with new value?

#!/usr/bin/perl my %RULES; open (RUL,"<","/home/stummala/a"); while (<RUL>) { my $line = $_; chomp $line; if ( /<(.*)>/ ) { my $num = $1; $dec_num = sprintf("%d", hex($num)); print "$dec_num\n"; } } close RUL;

Thanks in advance for your help.

  • Comment on How do i replace one variable with another after searching for a pattern
  • Download Code

Replies are listed 'Best First'.
Re: How do i replace one variable with another after searching for a pattern
by AppleFritter (Vicar) on Jul 31, 2014 at 18:20 UTC

    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.

      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;
Re: How do i replace one variable with another after searching for a pattern
by AppleFritter (Vicar) on Jul 31, 2014 at 19:07 UTC

    N.B. -- the OP let me know (via private message) that he'd also like to replace these angle-bracketed hex values with their corresponding ASCII characters. So here's the answer to answer that question as well:

    Just like above, except with this regex:

    s/<([0-9A-F]+)>/chr(hex($1))/eg;

      Thanks a lot, its working for me !!!

Re: How do i replace one variable with another after searching for a pattern
by CountZero (Bishop) on Aug 01, 2014 at 06:24 UTC
    I do hope the OP is now not going to use this solution to spam all and sundry with annoying home loan offers.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      Thats not for spamming, i work for an isp. We block spam here.