Loading ...
Sorry, an error occurred while loading the content.

Perl one-liner

Expand Messages
  • Bill Landry
    Hi List, First time poster here, and perl beginner. I have a question about a perl one-liner I am trying to put together to hex encode an ascii string, except
    Message 1 of 3 , May 18, 2009
    • 0 Attachment
      Hi List,

      First time poster here, and perl beginner. I have a question about a perl one-liner I am trying to put together to hex encode an ascii string, except for the text found inside braces "{}" or parenthesis "()"

      For example, I use the following perl one-liner:

      perl -pe 's/([a-fA-F0-9]{2})|(\{[^}]*\})|(\([^}]*\))/ defined $2 ? $2 : defined $3 ? $3 : chr(hex $1)/eg'

      to "decode" hex strings like the following (again, all but the text found between {} and ():

      582d4d61696c65723a205048504d61696c6572205b76657273696f6e20312e37335d{-3}582d4d61696c65723a207068706c6973742076322e31302e34{-3}582d4d65737361676549443a20(30|31|32|33|34|35|36|37|38|39)*3c6d65746120687474702d65717569763d22436f6e74656e742d547970652220636f6e74656e743d22746578742f68746d6c3b20636861727365743d69736f2d383835392d3122202f3e*3c626f6479206267636f6c6f723d222346464646464622206d617267696e6865696768743d223022206d617267696e77696474683d22302220746f706d617267696e3d223022206c6566746d617267696e3d223022206c696e6b3d2223333336363939223e

      and this works fine. However, I've only been able to come up with this one-liner to hex "encode" all data in an ascii input string:

      perl -pe 's/(.)/sprintf("%02lx", ord $1)/eg'

      But I cannot seem to come up with a solution that will allow me to bypass hex encoding the data inside () and {} for ascii strings like the following:

      X-Mailer: PHPMailer [version 1.73]{-3}X-Mailer: phplist
      v2.10.4{-3}X-MessageID: (30|31|32|33|34|35|36|37|38|39)*<meta
      http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />*<body
      bgcolor="#FFFFFF" marginheight="0" marginwidth="0" topmargin="0"
      leftmargin="0" link="#336699">

      I'm hoping someone here on the list might have some ideas or suggestions on how to accomplish this.

      Thanks!

      Bill
    • Robert Lee Binkley
      You are very close, but what you want is something like: perl -e while ( $ARGV[0] =~ /([a-fA-F0-9]{2})|( {[^}]* })|( ([^ )]* ))/g ) {print ( defined $2 ? $2 :
      Message 2 of 3 , May 19, 2009
      • 0 Attachment
        You are very close, but what you want is something like:



        perl -e 'while ( $ARGV[0] =~ /([a-fA-F0-9]{2})|(\{[^}]*\})|(\([^\)]*\))/g )
        {print ( defined $2 ? $2 : defined $3 ? $3 : chr(hex $1))};print qq[\n]'
        582d4d61696c65723a205048504d61696c6572205b76657273696f6e20312e37335d{-3}582d
        4d61696c65723a207068706c6973742076322e31302e34{-3}



        prints out



        -Mailer: PHPMailer [version 1.73]{-3}X-Mailer: phplist v2.10.4{-3}



        There are probably better ways ( I know there are ), but this does what
        you want!!

        Robert binkley



        From: perl-beginner@yahoogroups.com [mailto:perl-beginner@yahoogroups.com]
        On Behalf Of Bill Landry
        Sent: Monday, May 18, 2009 11:34 PM
        To: perl-beginner@yahoogroups.com
        Subject: [PBML] Perl one-liner








        Hi List,

        First time poster here, and perl beginner. I have a question about a perl
        one-liner I am trying to put together to hex encode an ascii string, except
        for the text found inside braces "{}" or parenthesis "()"

        For example, I use the following perl one-liner:

        perl -pe 's/([a-fA-F0-9]{2})|(\{[^}]*\})|(\([^}]*\))/ defined $2 ? $2 :
        defined $3 ? $3 : chr(hex $1)/eg'

        to "decode" hex strings like the following (again, all but the text found
        between {} and ():

        582d4d61696c65723a205048504d61696c6572205b76657273696f6e20312e37335d{-3}582d
        4d61696c65723a207068706c6973742076322e31302e34{-3}582d4d65737361676549443a20
        (30|31|32|33|34|35|36|37|38|39)*3c6d65746120687474702d65717569763d22436f6e74
        656e742d547970652220636f6e74656e743d22746578742f68746d6c3b20636861727365743d
        69736f2d383835392d3122202f3e*3c626f6479206267636f6c6f723d2223464646464646222
        06d617267696e6865696768743d223022206d617267696e77696474683d22302220746f706d6
        17267696e3d223022206c6566746d617267696e3d223022206c696e6b3d22233333363639392
        23e

        and this works fine. However, I've only been able to come up with this
        one-liner to hex "encode" all data in an ascii input string:

        perl -pe 's/(.)/sprintf("%02lx", ord $1)/eg'

        But I cannot seem to come up with a solution that will allow me to bypass
        hex encoding the data inside () and {} for ascii strings like the following:

        X-Mailer: PHPMailer [version 1.73]{-3}X-Mailer: phplist
        v2.10.4{-3}X-MessageID: (30|31|32|33|34|35|36|37|38|39)*<meta
        http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />*<body
        bgcolor="#FFFFFF" marginheight="0" marginwidth="0" topmargin="0"
        leftmargin="0" link="#336699">

        I'm hoping someone here on the list might have some ideas or suggestions on
        how to accomplish this.

        Thanks!

        Bill





        [Non-text portions of this message have been removed]
      • Bill Landry
        ... Thanks Robert, that does indeed work. ... However, someone else sent me this modification to what I already had, and it seems to be working just great:
        Message 3 of 3 , May 19, 2009
        • 0 Attachment
          Robert Lee Binkley wrote:

          > You are very close, but what you want is something like:
          >
          > perl -e 'while ( $ARGV[0] =~ /([a-fA-F0-9]{2})|(\{[^}]*\})|(\([^\)]*\))/g )
          > {print ( defined $2 ? $2 : defined $3 ? $3 : chr(hex $1))};print qq[\n]'
          > 582d4d61696c65723a205048504d61696c6572205b76657273696f6e20312e37335d{-3}582d
          > 4d61696c65723a207068706c6973742076322e31302e34{-3}
          >
          > prints out
          >
          > -Mailer: PHPMailer [version 1.73]{-3}X-Mailer: phplist v2.10.4{-3}

          Thanks Robert, that does indeed work.

          > There are probably better ways ( I know there are ), but this does what
          > you want!!

          However, someone else sent me this modification to what I already had,
          and it seems to be working just great:

          perl -pe 's/(\{[^}]*\}|\([^)]*\)|\*)|(.)/defined $1 ? $1 :
          sprintf("%02lx", ord $2)/eg'

          And I can pipe my data stings into it "|". But thanks again, your
          suggestion is very much appreciated!

          Bill
        Your message has been successfully submitted and would be delivered to recipients shortly.