How do I replace special character?

L

laredotornado

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
 
G

Guest

(e-mail address removed) 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.
 
T

Tad McClellan

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";
}
}
}
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Tad McClellan
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 said:
# 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top