1

I'm working on a sed script that creates a HTML file. I want to append paragraph tags to the beginning and ending of each line but can't figure out how that would work.

Right now I have the following sed script:

1i\
<html>\
<head><title>sed generated html</title></head>\
<body>\
<pre>
$a\
</pre>\
</body>\
</html>

How would I be able to enclose every line in a <p> and </p> tag?

Example:

test.txt

This is a test file.
This is another line in the test file.

Output with the sed script:

<html>
<head><title>sed generated html</title></head>
<body>
<pre>
<p>This is a test file.</p>
<p>This is another line in the test file.</p>
</pre>
</body>
</html>
user1804933
  • 453
  • 2
  • 7
  • 14

1 Answers1

3

With sed:

Change your script to:

1i\
<html>\
<head><title>sed generated html</title></head>\
<body>\
<pre>
s/.*/<p>&<\/p>/
$a\
</pre>\
</body>\
</html>

Same as your command, but added s/.*/<p>&<\/p>/. Surround each line in file with <p> and </p>. Use command sed -f script File

With awk:

cat script

BEGIN {
printf "<html>\n\
<head><title>sed generated html</title></head>\n\
<body>\n\
<pre>\n"
}
{print "<p>"$0"</p>"}
END {
printf "</pre>\n\
</body>\n\
</html>\n"
}

cat File

This is a test file.
This is another line in the test file.

Command:

awk -f script File

Sample:

AMD$ awk -f script File
<html>
<head><title>sed generated html</title></head>
<body>
<pre>
<p>This is a test file.</p>
<p>This is another line in the test file.</p>
</pre>
</body>
</html>
Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27