2014-12-13 145 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 
相关问题