我有兩個 HTML 檔案 -a.html和b.html. 我想將標題標簽替換b.html為a.html.
我們將如何使用 shell 腳本來做到這一點?我知道如何使用sed命令在同一個檔案中進行簡單替換。
一個.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document AAAAA</title>
</head>
<body>
This is Document A
</body>
</html>
b.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document BBBB</title>
</head>
<body>
This is Document B
</body>
</html>
b.html - 運行腳本后- 注意“Document BBBBB”更改為“Document AAAAA”
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document AAAAA</title>
</head>
<body>
This is Document B
</body>
</html>
uj5u.com熱心網友回復:
GNU sed:
title=`sed -n "s/^.*<title>\(.*\)<\/title>.*$/\1/p" a.html`; \
sed -i "s/^\(.*<title>\).*\(<\/title>.*\)$/\1$title\2/" b.html
BSD/OS X sed:
title=`sed -n "s/^.*<title>\(.*\)<\/title>.*$/\1/p" a.html`; \
sed -i '' "s/^\(.*<title>\).*\(<\/title>.*\)$/\1$title\2/" b.html
本質上,它用標題(組)a.html替換內容并將結果設定為變數。\1title
然后使用一個非常相似的正則運算式,它用b.html變數替換標題并保存b.html.
uj5u.com熱心網友回復:
sed -i 's@BBBBB@AAAAA@' b.html
那樣有用嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/521141.html
標籤:html壳嘘
