开发者

how to add \r\n in a variable in sed?

Simple question. When I use sed to add \r\n into the variable it fails. how to add \r\n?

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

sed: -e expression #1, char 146: unterminated `s' command

The whole code is here:

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


dat开发者_如何学运维eRecent=$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


Ppl can't re-create. So it might depends on sed version. New gnused should handle \r\n like some in comments reports.

Older gnused and other sed might need the original newline and return re-produced. Hence you can use echo to get it and neglect sed impplementions, but this brings another dependency on your 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 is easy, just double quote GNUSed is assumed.

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

See how life is a pain under AIX...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜