niyojet344
niyojet344

Reputation: 43

I keep on getting an error message when I try to read a file via awk. What went wrong?

I have 2 HTML files called test1.html and test2.html. The contents of the files are: test1.html

<body>
    <p>Hi!</p>
    <p>How are you?</p>
</body>

test2.html

<p>Hi!</p>

I am trying to execute the below awk command to replace <p>Hi!</p> in test1.html with the entire content of test2.html:

awk '{sub("<p>Hi!</p>",awk '{print}' test2.html); print}' test1.html

But I keep on getting a syntax error message "awk: line 1: syntax error at or near { awk: 1: unexpected character '.' awk: line 1: extra ')'". Is there any other way to read the content of test2.html?

Upvotes: 0

Views: 118

Answers (2)

M. Nejat Aydin
M. Nejat Aydin

Reputation: 10133

This task should be trivial with sed:

sed '/<p>Hi!<\/p>/{r test2.html
                   d
                  }' test1.html

Upvotes: 0

William Pursell
William Pursell

Reputation: 212404

It seems easiest to define a variable with the file content: eg

awk '{sub("<p>Hi!</p>", r)}1' r="$(cat test2.html)" test1.html

Upvotes: 1

Related Questions