2011-08-01 24 views
0

簡單的問題。 當我使用sed將\r\n添加到變量 時,它失敗。 如何添加\r\n如何在sed中的變量中添加 r n?

dateRecent=$(sed 's| 年| 年'"\r\n"'|g' <<< $newsDate) 
dateRecent=$(sed 's| 年| 年\r\n|g' <<< $newsDate) 

的sed:-e表達#1,炭146:無端接`s'的命令

整個代碼是在這裏:

cp /var/www/html/INFOSEC/textonly/sc_chi/anti/recent.html /var/www/html/INFOSEC/textonly/sc_chi/anti/recent.test.html 


echo "Please input Date in this format(eg.2011 年 7 月 8 日):" 
read -e newsDate 
echo "Please input Title:" 
read -e title 
echo "Please input Description:" 
read -e desc 
echo "Please input ID(eg.d071101):" 
read -e id 
echo "Please input reference website:" 
read -e web 
echo "Confirm? Have to do it all over again if wrong (Yes:y, No:n)" 
read -e confirm 


dateRecent=$newsDate 

if [[ "$mail" == "y" ]]; then 
     dateRecent=$(sed -e 's/ 年/ 年\r\n/g' <<< $newsDate) 
fi 
#Add Phishing attack in recent.html 
sed -i '0,/<li>/ { s/<li>/<li><a href="'"$web"'" target="_blank">'"$dateRecent"' - '"$title"'<\/a><\/li>\r\n        <li>/ }' /var/www/html/INFOSEC/textonly/sc_chi/anti/recent.test.html 
+0

此命令的作用(不發出錯誤並指定字符串後增加了一個新行)。 – whitequark

+0

是否因爲我使用讀取-e讀取輸入???因爲我看到錯誤消息表明sed:-e?我應該用什麼呢??謝謝 –

+0

也爲我工作。回聲asdffoobar | sed -e's/foo/\ r \ n/g'不打印任何警告 – mana

回答

1

PPL無法重新創建。所以它可能取決於sed版本。 新的gnused應該像評論報告中的一些一樣處理\ r \ n。

舊的gnused和其他sed可能需要原始換行並返回重新生成。因此你可以使用echo來獲取它並忽略sed impplementions,但是這會給你的shell帶來另一個依賴。

# ksh is my suggested standard style: 
sed "s/ 年/ 年`echo -e \\\r\\\n`/g" 
# zsh is like ksh and you can omit the -e for echo 

# Old bash?: 
sed 's/ 年'"/ 年`echo \\\r\\\n`/g" 

Windows很簡單,只需雙引號GNUSed即可。

sed "s/ 年/ 年\r\n/g" 

看到生命是AIX下的痛苦......我