I have two HTML files - a.html
and b.html
. I would like to replace the title tag in b.html
with the one in a.html
.
How would we do it using shell script? I know how to do a simple replacement in the same file by using sed
command.
a.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 - afer running the script - Notice "Document BBBBB" is changed to "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>
CodePudding user response:
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
Essentially, it takes a.html
, replaces the content with its title (\1
group) and sets the result to the title
variable.
Then with a very similar regex, it replaces the title in b.html
with the variable and saves b.html
.
CodePudding user response:
sed -i 's@BBBBB@AAAAA@' b.html
Does that work?