2010-06-24 104 views
5

我們有一個名爲LineFeed.sh的shell腳本文件,它具有將換行符(LF)轉換爲回車+換行符的功能。我們希望通過Windows中的批處理文件完成相同的操作。可能嗎?批處理文件+將LF轉換爲CR + LF

的Linux shell文件

E_WRONGARGS=65 
cat OutputList|while read -r Line 
do 
if [ -z "$Line" ] 
then 
echo "Usage: `basename $0` filename-to-convert" 
exit $E_WRONGARGS 
fi 
NEWFILENAME=$Line.unx 
CR='\015' # Carriage return. 
     # 015 is octal ASCII code for CR. 
     # Lines in a DOS text file end in CR-LF. 
     # Lines in a UNIX text file end in LF only. 
tr -d $CR < $1 > $NEWFILENAME // here its deleting CR but i need to append LF 
# Delete CR's and write to new file. 
done 
echo "Original DOS text file is \"$1\"." 
echo "Converted UNIX text file is \"$NEWFILENAME\"." 
exit 0 
+0

http://www.google.com/search?q=unix2dos.bat – Heinzi 2010-06-24 12:50:16

+2

@Heinzi:http://meta.stackexchange.com/questions/5280/embrace-the-non-googlers – Joey 2010-06-24 18:15:47

+0

@Johannes:其實,我的評論不只是谷歌的問題,但包含一個答案。是的,我過於簡潔。詳細版本可能是:「你自己不需要這樣做;有一個名爲unix2dos的腳本,它完全符合你的要求,並且有可用的Windows端口,通常稱爲」unix2dos.bat「。如果你是谷歌關鍵字,你會發現很多來源下載它。「 (不過,我確實明白你的觀點,感謝你的鏈接。) – Heinzi 2010-06-24 20:49:44

回答

13

您可以在this Wikipedia page找到一個辦法:

TYPE unix_file | FIND "" /V > dos_file 

請記住,你不能輸出重定向到你正在閱讀相同的文件。這適用於幾乎所有的系統和shell,因此需要額外的重命名。

這裏的關鍵是,type知道如何讀取LF行尾,然後find將轉換他們做CRLF。單獨不會對輸出做任何事情(它應該是這樣,因爲有一個簡單地將文件內容與它們混淆的命令是不好的:-))。

+3

您的觀點是? – Joey 2012-06-11 18:09:21