2013-10-30 148 views
0

現在我有這個shell代碼,我需要添加HTML標籤到簡單的文本。換句話說,我需要將文本格式化爲基本的HTML代碼。SHELL text to html

現在我有這個shell腳本:

#!/bin/sh 
# t2h {$1} html-ize a text file and save as foo.htm 
NL=" 
" 
cat $1 \ 
| sed -e 's/ at/at /g' \ 
| sed -e 's/[[:cntrl:]]/ /g'\ 
| sed -e 's/^[[:space:]]*$//g' \ 
| sed -e '/^$/{'"$NL"'N'"$NL"'/^\n$/D'"$NL"'}' \ 
| sed -e 's/^$/<\/UL><P>/g' \ 
| sed -e '/<P>$/{'"$NL"'N'"$NL"'s/\n//'"$NL"'}'\ 
| sed -e 's/<P>[[:space:]]*"/<P><UL>"/' \ 
| sed -e 's/^[[:space:]]*-/<BR> -/g' \ 
| sed -e 's/http:\/\/[[:graph:]\.\/]*/<A HREF="&">[&]<\/A> /g'\ 
           > foo.htm 

這裏是文本的例子:

"Greatest properly off ham exercise all. Unsatiable invitation its possession nor off. All difficulty estimating unreserved increasing the solicitude. Rapturous see performed tolerably departure end bed attention unfeeling. On unpleasing principles alteration of. Be at performed preferred determine collected. Him nay acuteness discourse listening estimable our law. Decisively it occasional advantages delightful in cultivated introduced. Like law mean form are sang loud lady put.  " 

"Greatest properly off ham exercise all. Unsatiable invitation its possession nor off. All difficulty estimating unreserved increasing the solicitude. Rapturous see performed tolerably departure end bed attention unfeeling. On unpleasing principles alteration of. Be at performed preferred determine collected. Him nay acuteness discourse listening estimable our law. Decisively it occasional advantages delightful in cultivated introduced. Like law mean form are sang loud lady put.  " 

,它需要是這樣的:

<html> 
<p>Greatest properly off ham exercise all. Unsatiable invitation its possession nor off. All difficulty estimating unreserved increasing the solicitude. Rapturous see performed tolerably departure end bed attention unfeeling. On unpleasing principles alteration of. Be at performed preferred determine collected. Him nay acuteness discourse listening estimable our law. Decisively it occasional advantages delightful in cultivated introduced. Like law mean form are sang loud lady put.</p><br /> 
<br /> 
<p>Greatest properly off ham exercise all. Unsatiable invitation its possession nor off. All difficulty estimating unreserved increasing the solicitude. Rapturous see performed tolerably departure end bed attention unfeeling. On unpleasing principles alteration of. Be at performed preferred determine collected. Him nay acuteness discourse listening estimable our law. Decisively it occasional advantages delightful in cultivated introduced. Like law mean form are sang loud lady put.</p> 
</html> 
+0

也許你可以使用降價? – BeniBela

+0

你的第一個「sed」命令實際上是在做什麼嗎?順便說一句,你有腳本,輸入和輸出,但你也有一個問題呢? – thom

回答

0

我看到你想用「sed」來替換換行符,無論出於何種原因。
這是行不通的。
「sed」對於換行符是盲目的,因爲「sed」是面向工作行的。 這意味着在開始評估之前,換行符被「sed」剝離。

最簡單的解決方法是在用「sed」開始之前,使用「tr」將每個換行符替換爲另一個字符。 這個其他字符不應該是可以在文本文件中找到的字符(或者您必須以某種方式構建自己的轉義序列)。

如果您更正了這些錯誤,但它仍然無效,請發佈您更正的代碼。
併發布您期望的每個sed命令。

P.S.用bash的本地字符串處理可能會更好。

0

嗨嘗試這樣的事情bash腳本數據文件與文本文件進行格式化

#!/bin/bash 
echo '<html>' >foo.htm 
cat DataFile |tr -d '"'|grep -v "^$" |\ 
while read Line 
do 
cat <<eof 
    <p>${Line}</p><br><br> 
eof 
done >>foo.htm 
echo '</html>' >>foo.htm