How do I find a 4 digit unicode character using this perl one liner?

Viewed 305

I have a file with this unicode character

enter image description here

File saved in notepad as UTF-8

I tried this line

C:\blah>perl -wln -e "/\x{1ed7}/ and print;" blah.txt

But it's not picking it up. If the file has a letter like 'a'(unicode hex 61), then \x{61} picks it up. But for a 4 digit unicode character, I have an issue picking up the character.

2 Answers

You had the right idea with using /\x{1ed7}/. The problem is that your regex wants to match characters but you're giving it bytes. You'll need to tell Perl to decode the bytes from UTF-8 when it reads them and then encode them to UTF-8 when it writes them:

perl -CiO -ne "/\x{1ed7}/ and print" blah.txt

The -C option controls how Unicode semantics are applied to input and output filehandles. So for example -CO (capital 'o' for 'output') is equivalent to adding this before the start of your script:

binmode(STDOUT, ":utf8")

Similarly, -CI is equivalent to:

binmode(STDIN, ":utf8")

But in your case, you're not using STDIN. Instead, the -n is wrapping a loop around your code that opens each file listed on the command-line. So you can instead use -Ci to add the ':utf8' I/O layer to each file Perl opens for input. You can combine the -Ci and the -CO as: -CiO

Your script works fine. The problem is the unicode you're using for searching. Since your file is utf-8 then your unique search parameters need to be E1, BB, or 97. Check the below file encoding and how that changes the search criteria.

 UTF-8 Encoding:    0xE1 0xBB 0x97
 UTF-16 Encoding:   0x1ED7
 UTF-32 Encoding:   0x00001ED7

Resource https://www.compart.com/en/unicode/U+1ED7

Related