Top Forums Shell Programming and Scripting Perl: encoding changes and odd symbols Post 303045758 by bedtime on Sunday 12th of April 2020 09:28:14 AM
Old 04-12-2020
Perl: encoding changes and odd symbols


This is a much simplified version of a script that I'm using. The program finds the number 1 in brackets-((1))-and replaces it with a sentence. The text is French because the program translates into French and I want to know it works properly with accents. (can just be pasted into shell):
rm -rf temp.tmp
touch temp.tmp
iconv -f utf-8 temp.tmp
echo '((1)) ((2))' >> temp.tmp
text='Il reçoit 5 000 $ à la livraison. 5 000 $?'
perl -i -CS -pne 's/\(\('"${num}"'\)\)/'"${text}"'/' temp.tmp
cat temp.tmp

The result is:

In an editor it displays as:

Il reçoit 5 000   la livraison. 5 000 0 ((2))

In shell it displays as:
$ cat temp.tmp.tmp
Il reçoit 5 000 � la livraison. 5 000 0 ((2))

The file was a UTF-8 before perl wrote on it and now it is iso-8859-1:
$ file -i temp.tmp
temp: text/plain; charset=iso-8859-1

I would like the result to be:
Il reçoit 5 000 $ à la livraison. 5 000 $? ((2))

It seems that after using echo the file format changes.

*** SOLUTION ***

I used sed instead and did this:

rm -rf temp.tmp
touch temp.tmp
iconv -f utf-8 temp.tmp
echo '((1)) ((2))' >> temp.tmp
text='Il reçoit 5 000 $ à la livraison. 5 000 $?'

LC_ALL=C sed -i 's/(('"${num}"'))/'"${text}"'/g' temp.tmp
cat temp.tmp

Second last line from the bottom works fine.

Last edited by bedtime; 04-12-2020 at 01:47 PM..

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers


Hi, I'm using putty and when I try to write it writes | (or when I try to write , it writes i) I tried to change settings/translation of putty but with no success I have KSH # locale LANG= LC_CTYPE="C" LC_NUMERIC="C" LC_TIME="C" LC_COLLATE="C" LC_MONETARY="C" LC_MESSAGES="C"... (3 Replies)
Discussion started by: palmer18
3 Replies

2. Shell Programming and Scripting

Perl IO vs GLOB symbols

Hi, Can someone please clarify how we are able to use both IO and GLOB symbols of a package variable interchangeably? Please consider the following code: open(FH,"myfile") || die "Unable to open file myfile:$@"; my $glob_var = *main::FH{GLOB}; my $io_var = *main::FH{IO}; print $glob_var... (0 Replies)
Discussion started by: srinivasan_85
0 Replies

3. AIX

get the file encoding

Hello! The system is AIX 5.3 Give please command or script to get the file encoding (1 Reply)
Discussion started by: vinment
1 Replies

4. Shell Programming and Scripting

Araic Encoding

hi folks , I have a shell script which contain SQL query that dump some data from the DB in arabic and this data is written to a file in unix machine but the problem that the arabic data is appear like ??????????|111|???????? even when I move it to my windows XP machine. Any one have an Idea... (2 Replies)
Discussion started by: habuzahra
2 Replies

5. Shell Programming and Scripting

Encoding troubles

Hello All I have a set of files, each one containing some lines that follows that regex: regex='disabled\,.*\,\".*\"'and here is what file says about each files: file <random file> <random file> ASCII text, with CRLF line terminatorsSo, as an example, here is what a file ("Daffy Duck - The... (3 Replies)
Discussion started by: tukuyomi
3 Replies

6. Shell Programming and Scripting

Encoding conversion in PERL script

I have oracle 9i database installed with UTF-8 Encoding. I want a perl script that converts unicode to utf8 before commiting in database and utf8 to unicode when retreiving from database For example : the word ntrnatnlztion has to be stored in database as Internationalization and when retreived... (6 Replies)
Discussion started by: vkca
6 Replies

7. Shell Programming and Scripting

Perl: How to Print symbols like " and ;

Hi, How do I print a line with symbols in a file? Exp: If I want to print line: Hi "Lisa;John" Command: print FILE "Hi "Lisa;John""; - will give me error Bareword found where operator expected... Can someone advise how can I print any line consiting symbols like example above. Thanks... (3 Replies)
Discussion started by: SSGKT
3 Replies

8. Shell Programming and Scripting

How to find the file encoding and updating the file encoding?

Hi, I am beginner to Unix. My requirement is to validate the encoding used in the incoming file(csv,txt).If it is encoded with UTF-8 format,then the file should remain as such otherwise i need to chnage the encoding to UTF-8. Please advice me how to proceed on this. (7 Replies)
Discussion started by: cnraja
7 Replies

9. Shell Programming and Scripting

Calling a Perl script in a Bash script -Odd Situation

I am creating a startup script for an application. This application's startup script is in bash. It will also need to call a perl script (which I will not be able to modify) for the application environment prior to calling the application. The problem is that this perl script creates a new shell... (5 Replies)
Discussion started by: leepet01
5 Replies

10. Solaris

View file encoding then change encoding.

Hi all!! Im using command file -i myfile.xml to validate XML file encoding, but it is just saying regular file . Im expecting / looking an output as UTF8 or ANSI / ASCII Is there command to display the files encoding? Thank you! (2 Replies)
Discussion started by: mrreds
2 Replies
encoding::warnings(3pm) 				 Perl Programmers Reference Guide				   encoding::warnings(3pm)

encoding::warnings - Warn on implicit encoding conversions VERSION
This document describes version 0.11 of encoding::warnings, released June 5, 2007. SYNOPSIS
use encoding::warnings; # or 'FATAL' to raise fatal exceptions utf8::encode($a = chr(20000)); # a byte-string (raw bytes) $b = chr(20000); # a unicode-string (wide characters) # "Bytes implicitly upgraded into wide characters as iso-8859-1" $c = $a . $b; DESCRIPTION
Overview of the problem By default, there is a fundamental asymmetry in Perl's unicode model: implicit upgrading from byte-strings to unicode-strings assumes that they were encoded in ISO 8859-1 (Latin-1), but unicode-strings are downgraded with UTF-8 encoding. This happens because the first 256 codepoints in Unicode happens to agree with Latin-1. However, this silent upgrading can easily cause problems, if you happen to mix unicode strings with non-Latin1 data -- i.e. byte-strings encoded in UTF-8 or other encodings. The error will not manifest until the combined string is written to output, at which time it would be impossible to see where did the silent upgrading occur. Detecting the problem This module simplifies the process of diagnosing such problems. Just put this line on top of your main program: use encoding::warnings; Afterwards, implicit upgrading of high-bit bytes will raise a warning. Ex.: "Bytes implicitly upgraded into wide characters as iso-8859-1 at - line 7". However, strings composed purely of ASCII code points (0x00..0x7F) will not trigger this warning. You can also make the warnings fatal by importing this module as: use encoding::warnings 'FATAL'; Solving the problem Most of the time, this warning occurs when a byte-string is concatenated with a unicode-string. There are a number of ways to solve it: o Upgrade both sides to unicode-strings If your program does not need compatibility for Perl 5.6 and earlier, the recommended approach is to apply appropriate IO disciplines, so all data in your program become unicode-strings. See encoding, open and "binmode" in perlfunc for how. o Downgrade both sides to byte-strings The other way works too, especially if you are sure that all your data are under the same encoding, or if compatibility with older versions of Perl is desired. You may downgrade strings with "Encode::encode" and "utf8::encode". See Encode and utf8 for details. o Specify the encoding for implicit byte-string upgrading If you are confident that all byte-strings will be in a specific encoding like UTF-8, and need not support older versions of Perl, use the "encoding" pragma: use encoding 'utf8'; Similarly, this will silence warnings from this module, and preserve the default behaviour: use encoding 'iso-8859-1'; However, note that "use encoding" actually had three distinct effects: o PerlIO layers for STDIN and STDOUT This is similar to what open pragma does. o Literal conversions This turns all literal string in your program into unicode-strings (equivalent to a "use utf8"), by decoding them using the specified encoding. o Implicit upgrading for byte-strings This will silence warnings from this module, as shown above. Because literal conversions also work on empty strings, it may surprise some people: use encoding 'big5'; my $byte_string = pack("C*", 0xA4, 0x40); print length $a; # 2 here. $a .= ""; # concatenating with a unicode string... print length $a; # 1 here! In other words, do not "use encoding" unless you are certain that the program will not deal with any raw, 8-bit binary data at all. However, the "Filter => 1" flavor of "use encoding" will not affect implicit upgrading for byte-strings, and is thus incapable of silencing warnings from this module. See encoding for more details. CAVEATS
For Perl 5.9.4 or later, this module's effect is lexical. For Perl versions prior to 5.9.4, this module affects the whole script, instead of inside its lexical block. SEE ALSO
perlunicode, perluniintro open, utf8, encoding, Encode AUTHORS
Copyright 2004, 2005, 2006, 2007 by Audrey Tang <>. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See <> perl v5.16.2 2012-08-26 encoding::warnings(3pm)

Featured Tech Videos

All times are GMT -4. The time now is 10:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy