Looking at your example, you might find it better to replace the "bad" character with a "good" equivalent. Maybe the thing between Y and S there should really be an apostrophe ("\x27") -- it's more and more common these days to find text data with "special" non-ASCII characters being used for quotes, apostrophes, hyphens and other basic ASCII punctuation marks. Just deleting them will tend to "damage" the text data.

Once you figure out exactly what these "bad" characters are and what they are supposed to mean, replacing them is easy. The first step is to get a proper look at them:

my %badchars; while (<>) { my @bad = ( /([^[:ascii:]])/g ); # find all bad characters if ( @bad ) { for my $badch ( @bad ) { $badchars{$badch}++; } my $badlist = join ' ', map { sprintf("%02x", ord{$_}) } @bad warn "line contains bad char(s) ($badlist): $_"; } } for my $badch ( sort keys %badchars ) { printf "%6d %02x %s\n", $badchars{$badch}, ord($badch), $badch; }
That will tell you (on STDOUT) which non-ASCII characters are in a given set of data, and how many of each. It also lists (on STDERR), the lines containing bad characters, and the hex codes for the bad characters on each line.

(If you prefer, you can add some code there to open two distinct output files yourself, write the "warn" messages to one of them and the "printf" output to the other.)

That will get you started, but if your input data is utf8 or some other multi-byte encoding, you need to know what encoding it is, and use perl to interpret it correctly as characters. I won't pursue this further, because you haven't given enough detail yet.

If you have more questions, come back with some specific details (the perl code you used, some actual input data, and the actual output you got).


In reply to Re: Checking for Valid Characters by graff
in thread Checking for Valid Characters by hozefa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.