转换后如何摆脱不可见字符
How to get rid of invisible characters after converting
我想将包含特殊撇号的 windows UTF8 文件转换为 unix ISO-8859-1 文件。我就是这样做的:
# -- unix file
tr -d '5' < my_utf8_file.xml > t_my_utf8_file.xml
# -- get rid of special apostrophe
sed "s/’/'/g" t_my_utf8_file.xml > temp_my_utf8_file.xml
# -- change the xml header
sed "s/UTF-8/ISO-8859-1/g" temp_my_utf8_file.xml > my_utf8_file_temp.xml
# -- the actual charecter set conversion
iconv -c -f UTF-8 -t ISO8859-1 my_utf8_file_temp.xml > my_file.xml
一切都很好,但我的一个文件中有一件事。文件的开头似乎本来就有一个不可见的字符。当我在 Notepadd ++ 中打开 my_file.xml 时,我在文件的开头看到了一个 SUB。在 Unix VI 中我看到 ^Z.
我应该在我的 unix 脚本中添加什么以及在哪里添加以删除这些类型的字符。
谢谢
要弄清楚你正在处理的是什么字符,请隔离有问题的行(在这种情况下,像 head -1 <file>
这样简单的东西就足够了)并将结果通过管道传递给 od
(使用适当的标志以所需格式显示字符):
head -1 <file> | od -c # view as character
head -1 <file> | od -d # view as decimal
head -1 <file> | od -o # view as octal
head -1 <file> | od -x # view as hex
一旦你知道你正在处理的角色,你就可以使用你最喜欢的命令(例如,tr
、sed
)来删除所述角色。
我想将包含特殊撇号的 windows UTF8 文件转换为 unix ISO-8859-1 文件。我就是这样做的:
# -- unix file
tr -d '5' < my_utf8_file.xml > t_my_utf8_file.xml
# -- get rid of special apostrophe
sed "s/’/'/g" t_my_utf8_file.xml > temp_my_utf8_file.xml
# -- change the xml header
sed "s/UTF-8/ISO-8859-1/g" temp_my_utf8_file.xml > my_utf8_file_temp.xml
# -- the actual charecter set conversion
iconv -c -f UTF-8 -t ISO8859-1 my_utf8_file_temp.xml > my_file.xml
一切都很好,但我的一个文件中有一件事。文件的开头似乎本来就有一个不可见的字符。当我在 Notepadd ++ 中打开 my_file.xml 时,我在文件的开头看到了一个 SUB。在 Unix VI 中我看到 ^Z.
我应该在我的 unix 脚本中添加什么以及在哪里添加以删除这些类型的字符。
谢谢
要弄清楚你正在处理的是什么字符,请隔离有问题的行(在这种情况下,像 head -1 <file>
这样简单的东西就足够了)并将结果通过管道传递给 od
(使用适当的标志以所需格式显示字符):
head -1 <file> | od -c # view as character
head -1 <file> | od -d # view as decimal
head -1 <file> | od -o # view as octal
head -1 <file> | od -x # view as hex
一旦你知道你正在处理的角色,你就可以使用你最喜欢的命令(例如,tr
、sed
)来删除所述角色。