LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-01-2017, 04:45 PM   #1
sjobs12
LQ Newbie
 
Registered: Jun 2017
Posts: 1

Rep: Reputation: Disabled
Replacing an ASCII 272 character with space


I have a file with a character that translates into 272 when I do:
od -xc

The character looks like an A with a caret on top with a degree sign to the right. I have tried sed, perl and tr and none of them are working.

For example:
sed -e 's/'$(echo "272")'/ /g' input_file > output_file

This does not work.

Any suggestions?
 
Old 06-01-2017, 06:37 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,731

Rep: Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125Reputation: 2125
That's because you are feeding in a string representation - you can use hex values directly in the sed substitution by using \x.. (or octal \o).
There something wrong if that (dec) number is supposed to be a single byte. Somebody that understands the myriad codepages might be able to explain that to you.
 
Old 06-01-2017, 07:08 PM   #3
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 142Reputation: 142
Code:
$ cat 272.pl
#!/usr/bin/perl
while ($_ = <>) {
   s/\\272//gs;
   print $_, "\n";
}

$ cat infile
Hello Wor\272ld! It's a \272Small World After\272 All! Though the oceans are wide and the mountains divide, \272It's \272A Small World After \272All!
$ ./272.pl < infile > outfile
$ cat outfile
Hello World! It's a Small World After All! Though the oceans are wide and the mountains divide, It's A Small World After All!
$

Last edited by Laserbeak; 06-01-2017 at 07:29 PM.
 
Old 06-02-2017, 12:20 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,194

Rep: Reputation: 961Reputation: 961Reputation: 961Reputation: 961Reputation: 961Reputation: 961Reputation: 961Reputation: 961
Let's note ASCII only covers codes 0-127. For 8 bit (0..255), there is ISO-8859-x (and many others). Above that there is Unicode.

http://www.unicode.org/charts/PDF/U0100.pdf

Code:
U+0110 Đ LATIN CAPITAL LETTER D WITH STROK
0x0110 = 272

Or, if 272 was meant to be hexadecimal:

http://www.unicode.org/charts/PDF/U0250.pdf

Code:
0272 ɲ LATIN SMALL LETTER N WITH LEFT HOOK
Anyways, you should first find out the actual file-encoding, eg:

Code:
$ printf 'szűrő' | od -tx1                   
0000000 73 7a fb 72 f5 # ISO-8859-2

$ printf 'szűrő' | iconv -f ISO-8859-2 -t UTF-8 | od -tx1
0000000 73 7a c5 b1 72 c5 91

$ printf 'szűrő' | iconv -f ISO-8859-2 -t UTF-16LE | od -tx1
0000000 73 00 7a 00 71 01 72 00 51 01

$ printf 'szűrő' | iconv -f ISO-8859-2 -t UTF-16BE | od -tx1
0000000 00 73 00 7a 01 71 00 72 01 51

Last edited by NevemTeve; 06-02-2017 at 01:00 AM.
 
Old 06-02-2017, 03:06 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,327
Blog Entries: 4

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
A single byte has the numeric range 0..255, and does not include characters such as the one you mention.

Therefore, we know that it is a Unicode character, and that it is being represented in the UTF-8 encoding scheme as (in this case ....) a pair of bytes.

Any UTF-aware "search and replace in a string" function should be able to accomplish this job trivially ... and, today, most of them are. (But you might in some cases have to select UTF-encoding.)
 
Old 06-02-2017, 03:13 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,154

Rep: Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362Reputation: 1362
The OP used od -xc which produces sequences of 4 hex digits, single letters, and 3 octal digits. Therefore, '272', being 3 digits, is most likely on octal number.

Code:
$ printf $'\272etc' | od -xc
0000000    65ba    6374
        272   e   t   c
0000004
 
1 members found this post helpful.
Old 06-02-2017, 08:46 PM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,327
Blog Entries: 4

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Quote:
Originally Posted by ntubski View Post
The OP used od -xc which produces sequences of 4 hex digits, single letters, and 3 octal digits. Therefore, '272', being 3 digits, is most likely on octal number.
(Very respectfully ...) The OP also originally described "the character that (s)he was actually seeing" as: "The character looks like an A with a caret on top with a degree sign to the right. Thus, I must assume that the reference is to the binary value of the first of two bytes which actually comprise the character. The WikiPedia article on 'UTF-8' describes this multi-byte encoding scheme in detail.

(Best-guess as to the actual character: "")

Last edited by sundialsvcs; 06-02-2017 at 08:48 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Down with Unicode! Why 16 bits per character is a right pain in the ASCII LXer Syndicated Linux News 1 10-04-2013 02:06 PM
fvwm truncates titlebar text at first non-ascii character shachter Linux - Desktop 3 04-23-2013 05:08 PM
ASCII file. Replacing '[TEX]' with '$'. stf92 Linux - Newbie 6 04-17-2012 03:14 PM
To know the function on checking whether a character is ascii or unicode in C. murugesan Programming 3 01-23-2009 10:51 PM
To know the function on checking whether a character is ascii or unicode character. murugesan Programming 2 01-23-2009 01:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration