Here are some options to convert all decomposed Unicode characters into their precomposed form. I'll assume that the files are encoded in UTF-8.
iconv
iconv
is a character set conversion tool (based on the iconv
codeset conversion functions). On macOS, it supports the "UTF-8-MAC" encoding for decomposed UTF-8 characters (see What the difference and usage of encodings UTF-8 and UTF-8-MAC in iconv?).
Usage:
iconv -f UTF-8-MAC -t UTF-8 < decomposed.txt > composed.txt
I found that iconv
has problems with characters outside of the basic multilingual plane (emojis, flags, ...). As an example, it can not convert the characters 𐂃 or 😀. The following solutions do not have that problem.
Perl
A simple Perl script (found here on Stack Overflow) can convert decomposed to precomposed UTF-8:
#!/usr/bin/perl
use Encode qw/decode_utf8 encode_utf8/;
use Unicode::Normalize;
while (<>) {
print encode_utf8( NFC(decode_utf8 $_) );
}
Usage:
perl compose.pl < decomposed.txt > composed.txt
Perl has also the -i
option to modify a file “in situ”:
perl -i compose.pl file.txt
uconv
(Found here on the Unix & Linux Stack Exchange.) uconv
is not part of macOS, but can be installed from Homebrew (brew install icu4c
) or Macports (port install icu
).
Usage:
uconv -x any-nfc < decomposed.txt > composed.txt
Swift
If you have Xcode installed on your computer then a simple Swift script does the trick:
#!/usr/bin/swift
import Foundation
while let line = readLine() {
print(line.precomposedStringWithCanonicalMapping)
}
Usage:
swift compose.swift < decomposed.txt > composed.txt
iconv -f UTF-8 -t UTF-8-MAC filename > filename2
should do the trick, see apple.stackexchange.com/q/346453/30895