2

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>

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Tested and it works as expected. Thanks for your explanation as well. Do you have any recommended resources to learn shell script and regex for beginner? Thanks
I won't pretend to be a shell scripting expert. It's just some past experience combined with stack overflow collective wisdom )
0
sed -i 's@BBBBB@AAAAA@' b.html

Does that work?

1 Comment

The example is a simplify version. I don't know what is in a.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.