2014-06-30 93 views
1

我有utf-8與俄羅斯字符我嘗試複製到新文件 但結果是亂碼字符。 源文件可以打開並在vim中看到很好。 這是我的腳本的一部分,Linux csh,從utf-8文件複製文本到其他文件結果亂碼

# this is how i found in the internet to convert file to utf-8 
set new_file_full_path = "/home/foo/env/output/test.csv" 
set f = "/home/foo/env/source/source_test.txt" 
if(! -e $new_file_full_path) then 
     touch $new_file_full_path 
     iconv -f latin1 -t utf8 $new_file_full_path 
endif 
set new_line_buffer = "" 
foreach line("`cat $source_dir/$f`") 
#echo $line 
if($i > 14) then 
    echo "about to append to file" 
    echo $new_file_full_path 
    echo $line >> $new_file_full_path 
    echo "done append to file" 
endif 
@ i = $i + 1 
end 

我認爲問題的一部分是,當我這樣做:

目標文件

file -i /home/foo/env/output/test.csv 
/home/foo/env/output/test.csv: text/plain; charset=unknown-8bit terminators 

和源文件

file -i /home/foo/env/source/source_test.txt 
text/plain; charset=utf-8 

因爲您看到目標文件並非utf-8文件,即使我嘗試將其轉換爲

回答

0

請注意,如果要在源文件和目標文件中保留UTF-8,則不需要iconv

iconv -f latin1 -t utf8 $new_file_full_path 

這裏你問一個轉換 LATIN1 文件/home/foo/env/output/test.csv的UTF-8,結果應以stdout出現你沒有指定輸出文件。

我認爲,你想要的東西,如:

iconv -f utf8 -t latin1 $f -o $new_file_full_path 

隨着UTF-8 $f源文件,並得到轉換的$new_file_full_path,在latin1的,如果它是你想要的。

+0

它不工作我曾經做過什麼 – user63898

+0

對不起,我沒有意識到:[拉丁1](http://en.wikipedia.org/wiki/ISO/IEC_8859-1)可能不是您想要的編碼文件:它不支持俄文字符。您可能想嘗試[ISO_8859-5](http://en.wikipedia.org/wiki/ISO/IEC_8859-5)。 順便說一句,我不知道你在'endif'之後做什麼... – Coconop

相關問題