我有一个文件包含许多带变音符号的元音.我需要做这些替换:
我知道我可以一次更换一个:
sed -i 's/?/a/g' ./file.txt
Run Code Online (Sandbox Code Playgroud)
有没有更有效的方法来取代所有这些?
Ken*_*ent 53
如果您查看该工具的手册页iconv:
// TRANSLIT
当字符串"// TRANSLIT"附加到--to-code时,将激活音译.这意味着当角色无法在目标字符集中表示时,可以通过一个或多个相似的字符来近似.
所以我们可以这样做:
kent$ cat test1
Replace ?, á, ?, and à with a.
Replace ?, é, ?, and è with e.
Replace ?, í, ?, and ì with i.
Replace ?, ó, ?, and ò with o.
Replace ?, ú, ?, and ù with u.
Replace ?, ?, ?, and ? with ü.
Replace ?, Á, ?, and À with A.
Replace ?, É, ?, and È with E.
Replace ?, Í, ?, and Ì with I.
Replace ?, Ó, ?, and Ò with O.
Replace ?, Ú, ?, and Ù with U.
Replace ?, ?, ?, and ? with Ü.
kent$ iconv -f utf8 -t ascii//TRANSLIT test1
Replace a, a, a, and a with a.
Replace e, e, e, and e with e.
Replace i, i, i, and i with i.
Replace o, o, o, and o with o.
Replace u, u, u, and u with u.
Replace u, u, u, and u with u.
Replace A, A, A, and A with A.
Replace E, E, E, and E with E.
Replace I, I, I, and I with I.
Replace O, O, O, and O with O.
Replace U, U, U, and U with U.
Replace U, U, U, and U with U.
Run Code Online (Sandbox Code Playgroud)
pot*_*ong 10
这可能对你有用:
sed -i 'y/?á?à?é?è?í?ì?ó?ò?ú?ù?????Á?À?É?È?Í?Ì?Ó?Ò?Ú?Ù????/aaaaeeeeiiiioooouuuuüüüüAAAAEEEEIIIIOOOOUUUUÜÜÜÜ/' file
Run Code Online (Sandbox Code Playgroud)
我喜欢iconv它处理所有口音变化:
cat non-ascii.txt | iconv -f utf8 -t ascii//TRANSLIT//IGNORE > ascii.txt
Run Code Online (Sandbox Code Playgroud)