How do I replace special character?

Discussion in 'Perl Misc' started by laredotornado@zipmail.com, Apr 18, 2006.

  1. Guest

    Hello,

    I'm on Solaris and trying to use a Perl command to replace some
    untypeable charcters in a text document. When I open the document in
    "vi", I see a couple of strange characters -- "~Q" (a single character,
    not the combination of "~" and "Q") and "~R". How do I find out the
    hex codes for these characters, and then how do I use the following
    command

    perl -pi -e 's/pattern_to_search/pattern_to_replace/g' myfile.txt

    to replace them?

    Thanks, - Dave
     
    , Apr 18, 2006
    #1
    1. Advertising

  2. Guest Guest

    wrote:
    : Hello,

    : I'm on Solaris and trying to use a Perl command to replace some
    : untypeable charcters in a text document. When I open the document in
    : "vi", I see a couple of strange characters -- "~Q" (a single character,
    : not the combination of "~" and "Q") and "~R". How do I find out the
    : hex codes for these characters, and then how do I use the following
    : command

    Looks a bit like old IBM cp437 coded material opened in a Latin1 environ-
    ment. If you have any incarnation of vim, placing your cursor over the
    character in question; hitting 'g' 'a' will show you the ascii, hex and
    octal values of the character under the cursor.

    : perl -pi -e 's/pattern_to_search/pattern_to_replace/g' myfile.txt

    Say \x{<number>} in pattern_to_search for each character you want to
    replace. Obtain <number> with the vi(m) command just mentioned.

    Oliver.

    --
    Dr. Oliver Corff e-mail: -berlin.de
     
    Guest, Apr 18, 2006
    #2
    1. Advertising

  3. <> wrote:

    > trying to use a Perl command to replace some
    > untypeable charcters in a text document.



    Many years ago I wrote a little utility to expose "funny characters"
    when I encountered a similar situation. (it is pretty slow though.)

    Here it is.

    -----------------------------
    #!/usr/bin/perl
    # list_control_chars - output the values of control chars
    use strict;
    use warnings;

    while (<>) {
    foreach my $char (split //) {
    my $chnum = ord($char);

    if ( ($chnum >= 32 && $chnum <= 126) ||
    $chnum == 9 ||
    $chnum == 10 ) {
    # do nothing
    }
    else {
    my $hex = sprintf '0x%X', $chnum;
    my $oct = sprintf '0%o', $chnum;
    print "$chnum $oct ($hex)\n";
    }
    }
    }
    -----------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 18, 2006
    #3
  4. Jim Gibson <> wrote:


    > On unix, use the od (octal dump) program to dump files by octal or hex



    On any OS that has perl installed, you can use xdump to dump files by hex.

    xdump was sample code given in the 1st edition of the Camel book (p272).


    Google found it, of course, though from a different book:

    http://www.cpan.org/scripts/nutshell/ch6/xdump


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 19, 2006
    #4
  5. [A complimentary Cc of this posting was sent to
    Tad McClellan
    <>], who wrote in article <>:
    > Many years ago I wrote a little utility to expose "funny characters"
    > when I encountered a similar situation. (it is pretty slow though.)


    Should be trivial to speed up.

    > while (<>) {
    > foreach my $char (split //) {
    > my $chnum = ord($char);
    >
    > if ( ($chnum >= 32 && $chnum <= 126) ||
    > $chnum == 9 ||
    > $chnum == 10 ) {


    13 is not funny too; but your code is not in binmode, so you should
    not see it; however, then on \cZ + Dosish you get unexpected stuff.

    Better binmode (not simple with <> ?), and skip 13 as well...

    > # do nothing
    > }
    > else {
    > my $hex = sprintf '0x%X', $chnum;
    > my $oct = sprintf '0%o', $chnum;
    > print "$chnum $oct ($hex)\n";
    > }
    > }
    > }


    printf "%#07d %#07x %#07o\n", ($1) x 3 while /([^ -\x7E\r\n\t])/g;

    Hope this helps,
    Ilya
     
    Ilya Zakharevich, Apr 19, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    7,994
    Martin Honnen
    Apr 26, 2005
  2. mr h q
    Replies:
    5
    Views:
    15,212
    Karl Heinz Buchegger
    Nov 25, 2003
  3. John_Woo
    Replies:
    4
    Views:
    668
    shakah
    Jul 6, 2006
  4. Joseph Kesselman

    Re: replace special characters

    Joseph Kesselman, Jan 11, 2007, in forum: XML
    Replies:
    0
    Views:
    482
    Joseph Kesselman
    Jan 11, 2007
  5. herman
    Replies:
    5
    Views:
    7,613
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 30, 2007
Loading...

Share This Page