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

Hi, I have this file and somehow these control characters and these ~A, ~D characters appeared in the file. I want to do a search and remove these characters. What are these ~A, ~D characters? I open up on vim and they are highlighted in unique blue.
  • Comment on search and replace these unique characters

Replies are listed 'Best First'.
Re: search and replace these unique characters
by toolic (Bishop) on Dec 04, 2007 at 14:56 UTC
    I'm not sure what those characters are, but when I am trying to identify unusual characters, I use the ord function:
    #!/usr/bin/env perl use warnings; use strict; my $str = 'abcde'; for (split //, $str) { print "$_:", ord $_, ":\n"; }
    Once you identify the characters, it will be easier to figure out a way of eliminating them.
      Somehow, character code point numbers are always easier for me to grok (and look up on code charts) when shown in hex; also, it can help to put in a "safe" placeholder for the non-printable ones -- otherwise, they can sometimes make the display somewhat confusing:
      for (split //, $str) { printf( "%s: %02x\n", (/[[:print:]]/) ? $_:".", ord()); }
Re: search and replace these unique characters
by tuxz0r (Pilgrim) on Dec 04, 2007 at 15:12 UTC
    If you're in *nix land, you can also try cat -vet and od commands to look for strange characters in files.

    ---
    echo S 1 [ Y V U | perl -ane 'print reverse map { $_ = chr(ord($_)-1) } @F;'
    Warning: Any code posted by tuxz0r is untested, unless otherwise stated, and is used at your own risk.

Re: search and replace these unique characters
by Your Mother (Archbishop) on Dec 04, 2007 at 18:07 UTC

    You can also try something like this (do NOT try it on the original until you verify it works the way you want on a copy).

    perl -pi -e 's/[^\s[:print:]]//g' [filename]

Re: search and replace these unique characters
by leocharre (Priest) on Dec 04, 2007 at 16:26 UTC
    I have a mediocre solution for this. Enter visual mode, highlight the chars in question. Enter command mode..
    :%s/$action//g
    where it says $action, paste, middle click- on linux.
    I know that either in visual or insert, hihghlighting will NOT work on these characters.
    I think all this happens because of ASCII / UTF8 conversion.. Again, I my 'solution' is mediocre, but it should help you look into it deeper if you so desire.