2014-12-13 68 views
-3

請幫忙。我需要在這個Unix類的下午4點之前把它轉換成。自昨晚7點以來,我一直在努力。沒睡過。這個任務有三部分。我只需要最後一部分的幫助。如果我不能完成這個,我就會失敗。將該文件中的CR/LF行結束符轉換爲Unix樣式的LF行結束符?

階段3

在同一目錄中,寫一個腳本asciiFix.sh這需要通過命令行的文件路徑的任意數量,並且執行對每一個相同的分析。如果一個文件不是Windows ASCII,你的腳本不應該對它做任何事情。對於在Windows ASCII的每個文件,腳本應該打印消息

轉換文件名

,並應然後轉換CR/LF行終止在該文件中,以Unix風格的LF行終止。

例如:

cp ~cs252/Assignments/ftpAsst/d3.dat wintest.txt 
./asciiFix.sh /usr/share/dict/words wintest.txt fileType.sh 
converting wintest.txt 
and, after the script has finished, you should be able to determine that wintest.txt is now a  Unix ASCII file. 

當你認爲你有你的腳本工作,運行

〜cs252 /斌/ scriptAsst.pl 如果所有三個腳本工作正常,您將收到您訪問代碼。

我Attemps:

#!/bin/sh 
for file in "[email protected]" do 
    if file "$file" | grep "ASCII text, with CRLF"; then 
    echo "converting $file" 
    sed -e s/[\\r\\n]//g "$file" 
fi  
done 

結果:

./asciiFix.sh: 3: ./asciiFix.sh: Syntax error: "if" unexpected (expecting "do") 
    aardvark.cpp /home/cs252/Assignments/scriptAsst/winscrubbed.dat differ: byte 50, line 1 

    Failed: incorrect file conversion when running ./asciiFix.sh 'aardvark.cpp' 'bongo.dat' '  cat.dog.bak 

㈡曾試圖取出,如果再。我已經嘗試過sed -i's/^ M // g'「$ file」,還使用了dos2unix,以及其他一些我不記得的東西。但它總是說這些文件的轉換不正確。

加入後;並切換到DOS2UNIX的:

#!/bin/sh 
for file in "[email protected]"; 
do 
    if file "$file" | grep "ASCII text, with CRLF"; then 
    echo "converting $file" 
    dos2unix "$file" 
fi  
done 

的錯誤,我現在得到:

dos2unix: converting file aardvark.cpp to Unix format ... 

Failed when running: ./asciiFix.sh 'aardvark.cpp' 'bongo.dat' 'cat.dog.bak' 
+0

4pm。哪個時區,哈哈? (你還活在光盤上嗎?) – 2014-12-13 13:32:32

+0

該作業包含一個不精確的術語「Windows ASCII」。 ASCII是一個定義良好的字符集,但不是(通常)Windows上正在使用的字符集。這裏的意圖可能是[代碼頁1252](http://en.wikipedia.org/wiki/Windows-1252),它是西方Windows安裝中的默認字符集,但它絕不是唯一可能的解釋。當然,他們似乎只關心文件中的行結尾,而不關心它使用的ASCII超集。 – tripleee 2014-12-13 13:43:10

+0

東部時間下午4點 – purpleminion 2014-12-13 17:44:18

回答

0

do之前忘了;do算作新的陳述。或者,您可以將do放在新行上。在我看來,將DOS行結束符(CRLF)轉換爲Unix行結束符(僅LF)最舒適的方式是dos2unix。如果您修復了您的;錯誤,則使用dos2unix而不是sed應該是直截了當並且微不足道的。

+0

添加;幫助,我切換到dos2Unix。但是我仍然收到一個錯誤。 – purpleminion 2014-12-13 17:50:31

1

感謝您的幫助。 終於工作的代碼是:

#!/bin/sh 
for file in "[email protected]"; 
do 
    if file "$file" | grep -q "ASCII text, with CRLF"; then 
     echo "converting $file" 
     dos2unix "$file" 
    fi 
done 
+0

請將此(或另一個)答案標記爲已接受,以便此問題不再顯示爲未解決問題。謝謝。 – tripleee 2014-12-13 20:28:58

0

由於DOS2UNIX的7.1,您可以使用DOS2UNIX的本身來測試CRLF。這樣你不僅限於ASCII。

#!/bin/sh 
for file in "[email protected]"; 
do 
    if [ -n "$(dos2unix -ic $file)" ]; then 
     echo "converting $file" 
     dos2unix "$file" 
    fi 
done 
相關問題