Convert UTF-8 file to ASCII/ISO8859-1 OR replace characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert UTF-8 file to ASCII/ISO8859-1 OR replace characters
# 1  
Old 08-30-2016
Convert UTF-8 file to ASCII/ISO8859-1 OR replace characters

I am trying to develop a script which will work on a source UTF-8 file and perform one or more of the following
It will accept the target encoding as an argument e.g. US-ASCII or ISO-8859-1, etc
1. It should replace all occurrences of characters outside target character set by " " (space) or whatever character we define. Naturally we don't want to shift character positions in case of fixed width files.
2. It should also have the ability to get rid of characters altogether, characters which fall outside target character set.
3. It should have the ability to translate certain characters .e.g. "¥" to "JPY" or "£" to "GBP" or "Â" to "A"

What I have tried
1. This is good to get rid of characters, but it doesn't work for fixed width. Further it has //TRANSLIT option but one has no control over character translation.

Code:
 iconv -f UTF-8 -t US-ASCII//IGNORE -c utf8_file.txt


2. This gives me a list of characters > \xff

Code:
 echo "!\"#$%&0@ABab£¥§©ほぼぽま~अ" | grep -P -o '[^\x00-\xff]'
ほ
ぼ
ぽ
ま
अ

Trying to replace them with a space or any other character.

Code:
 echo "!\"#$%&0@ABab£¥§©ほぼぽま~अ" | sed 's/[^\x00-\xff]/ /g'

Error: "sed: -e expression #1, char 18: Invalid collation character"

Even sed -e option doesn't work here, same error.

Using the hexdump

Code:
 echo -n अ | hexdump -ve '1/1 " %.2x"' 
Output: e0 a4 85

If I plug in this value in sed this works.
Code:
 echo "!\"#$%&0@ABab£¥§©ほぼぽま~अ" | sed 's/\xe0\xa4\x85/" "/g'
Output: !"#$%&0@ABab£¥§©ほぼぽま~" "

So my final approach is parse the file and use grep to find characters outside the range of target encoding and put only matching characters into a temp file with their hexdump values

Code:
grep -P -o '[^\x00-\x7f]' utf_sample.txt -> temp file

Then loop through temp file and for each character do the character replacement or suppress characters one by one.

I am stuck here
1. Is there a way to define hexdump values of characters in a variable. This will be used in association with grep command to create temp file
below does not work for me.

Code:
hex_range=$'\x00-\xff'
echo "!\"#$%&0@ABab£¥§©ほぼぽま~अ" | grep -P -o '[^$(hex_range)]'

OR

Code:
 echo "!\"#$%&0@ABab£¥§©ほぼぽま~अ" | grep -P -o '[^`eval $hex_range`]'

2. Is there a way to store the hex value in some variable and use sed or anything else to perform find replace?
here find_char = each character read from temp file one by one
Even this does not work.
Code:
 find_char=`echo -n अ | hexdump -ve '1/1 " %.2x"' | sed 's/ /\\\x/g'`
echo "!\"#$%&0@ABab£¥§©ほぼぽま~अ" | sed 's/$find_char/ /g'

I have tried lot of options and googled at least 100 webpages to get some clues. I am open to ideas, suggestions...

I am using
Code:
$ uname -a
Linux <server_name> 2.6.32-573.26.1.el6.x86_64 #1 SMP Tue Apr 12 01:47:01 EDT 2016 x86_64 x86_64 x86_64 GNU/Linux
$ locale
LANG=C
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 08-31-2016 at 03:46 AM.. Reason: Changed CODE tags.
# 2  
Old 08-31-2016
Everyone at the UNIX and Linux Forums gives their best effort to reply to all questions in a timely manner. For this reason, posting questions with subjects like "Urgent!" or "Emergency" or "Help Me!" and demanding a fast reply are not permitted in the regular forums.

For members who want a higher visibility to their questions, we suggest you post in the Emergency UNIX and Linux Support Forum. This forum is given a higher priority than our regular forums.

Posting a new question in the Emergency UNIX and Linux Support Forum requires forum Bits. We monitor this forum to help people with emergencies, but we do not not guarantee response time or best answers. However, we will treat your post with a higher priority and give our best efforts to help you.

If you have posted a question in the regular forum with a subject "Urgent" "Emergency" or similar idea, we will, more-than-likely, close your thread and post this reply, redirecting you to the proper forum.

Of course, you can always post a descriptive subject text, remove words like "Urgent" etc. (from your subject and post) and post in the regular forums at any time.


Thank you.

The UNIX and Linux Forums
# 3  
Old 08-31-2016
Neo thatks for pointing out. I did go over the forum rules but looks like I overlooked. Btw, I am trying to edit the subject with no success.
# 4  
Old 08-31-2016
That is a complex specification to read (and understand). If I did - which I don't dare to assert - a solution to replace "extended" characters with "nomal" ones would be
Code:
echo "\"#$%&0@ABab£¥§©ほぼぽま~अ" | LC_ALL=C sed 'p; s/[\xe0-\xef]../X/g; s/[\xc0-\xcf]./Y/g'
"#$%&0@ABab£¥§©ほぼぽま~अ
"#$%&0@ABabYYYYXXXX~X

Here the characters in question are replaced by X or Y, depending on their representation in UTF-8 - replace them by what you want, e.g. spaces or nothing. If you want to translate to EUR, do so BEFORE those substitutions above, but be aware that then your "fixed length" will be lost.

Last edited by RudiC; 08-31-2016 at 04:55 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Hex to Ascii in a Ascii file

Hi All, I have an ascii file in which few columns are having hex values which i need to convert into ascii. Kindly suggest me what command can be used in unix shell scripting? Thanks in Advance (2 Replies)
Discussion started by: HemaV
2 Replies

2. Shell Programming and Scripting

Search and Replace Extended Ascii Characters

We are getting extended Ascii characters in the input file and my requirement is to search and replace them with a space. I am using the following command LANG=C sed -e 's// /g' It is doing a good job, but in some cases it is replacing the extended characters with two spaces. So my input... (12 Replies)
Discussion started by: ysvsr1
12 Replies

3. Linux

Help to Convert file from UNIX UTF-8 to Windows UTF-16

Hi, I have tried to convert a UTF-8 file to windows UTF-16 format file as below from unix machine unix2dos < testing.txt | iconv -f UTF-8 -t UTF-16 > out.txt and i am getting some chinese characters as below which l opened the converted file on windows machine. LANG=en_US.UTF-8... (3 Replies)
Discussion started by: phanidhar6039
3 Replies

4. Red Hat

Can't convert 7bit ASCII to UTF-8

Hello, I am trying to convert a 7bit ASCII file to UTF-8. I have used iconv before though it can't recognize it for some reason and says unknown file encoding. When I used ascii2uni package with different package, ./ascii2uni -a K -a I -a J -a X test_file > new_test_file It still... (2 Replies)
Discussion started by: rockf1bull
2 Replies

5. Shell Programming and Scripting

Remove characters other than ISO8859-1

Hi please help in writing a script for replacing all the non-iso8859-1 characters to question marks. I need a pattern of this kind "sed s/<non-iso char range>/?/g < ipfile > opfile" Please help me in this. (2 Replies)
Discussion started by: rprajendran
2 Replies

6. Shell Programming and Scripting

convert ascii values into ascii characters

Hi gurus, I have a file in unix with ascii values. I need to convert all the ascii values in the file to ascii characters. File contains nearly 20000 records with ascii values. (10 Replies)
Discussion started by: sandeeppvk
10 Replies

7. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

8. Shell Programming and Scripting

replace UTF-8 characters with tr

Hi, I try to get tr to replace multibytes characters by ascii equivalent. For example "Je vais à l'école" ---> 'Je vais a l'ecole" But my version of tr (5.97) doesn't seem to support multibyte sets. $ locale charmap; echo "Je vais à l'école" | tr éà ea UTF-8 Je vais aa l'aacole I try to... (2 Replies)
Discussion started by: ripat
2 Replies

9. Shell Programming and Scripting

Replace characters in a string using their ascii value

Hi All, In the HP Unix that i'm using when i initialise a string as Stalled="'30¬G'" Stalled=$Stalled" '30¬C'", it is taking the character ¬ as a comma. I need to grep for 30¬G 30¬C in a file and take its count. But since this character ¬ is not being understood, the count returns a zero. The... (2 Replies)
Discussion started by: roops
2 Replies

10. Programming

Howto convert Ascii -> UTF-8 & back C++

While working with russian text under FreeBSD&MySQL I need to convert a string from MySQL to the Unicode format. I've just started my way in C++ under FreeBSD , so please explain me how can I get ascii code of Char variable and also how can i get a character into variable with the specified ascii... (3 Replies)
Discussion started by: macron
3 Replies
Login or Register to Ask a Question