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

Time:12-29

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?

CodePudding user response:

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

CodePudding user response:

This task should be trivial with sed:

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