2017-10-20 144 views
0

我想使用sed將txt文件轉換爲html。如何在sed中使用unicode?

但是,要匹配html語法,我需要包含標籤(因此也包括<和>)。當我在sed表達式中使用這些字符時,sed認爲我指定了源文件或目標文件,即使我將它們轉義。我不斷收到消息「系統找不到指定的文件」。

我該如何避免這種情況?我可以以某種方式使用unicode號碼嗎?

源文件:input.txt中

內容:

Hello world! 

期望中的目標文件:output.htm

內容:

<html><body>Hello world!</body></html> 

sed命令不工作:

sed -r 's#(.*)#\<html\>\<body\>\1\<\/body\>\<\/html\>#g' <input.txt >output.htm 
+0

這種簡單的情況下你不需要的sed – RomanPerekhrest

+1

嘛,['sed的-r的## \ 1#g''(*)(https://開頭ideone。 com/dpMnsC)的作品。 –

回答

2

通過簡單shellprintf功能:

printf "<html><body>%s</body></html>\n" "$(< input.txt)" > output.htm 

output.htm內容:

<html><body>Hello world!</body></html> 

如果您仍然需要sed方法(通過某些目的) :

echo -e "<html><body>\n</body></html>" | sed '1 r input.txt' > output.htm 
  • 1 r input.txt - 在這裏r命令將讀取並通過HTML內容的1第一行(行由\n分隔)

output.htm內容後插入的input.txt內容:

<html><body> 
Hello world! 
</body></html> 
0

你可以保持更多的SIM卡如下所示。

echo "<html><body>" && cat Input_file && echo "</body></html>" 

輸出如下。

<html><body> 
Hello world! 
</body></html>