2

I have a HTML file which gets generated by some tool. However, I need to use Perl to inset a newline after each line.

In a browser, if I was to view the source of this file, it would look something like this:

<body>
This is line number one
This is another line
This is also
Another line
</body>

Obviously this is just a snippet of the file. However, I think I would need to open this file and insert
at the end of each line in order to format it, so that when viewed in a browser, it looks "nice".

Is this the most sensible / efficient method to employ?

Thank you.

3
  • Lines are defined by having newlines at the end of them. If there are no newlines, there is only one line, and inserting a newline at the end of it doesn't help you much. If newlines are already there, you don't need to insert them. I guess I have no idea what you're asking. Commented Dec 5, 2010 at 0:23
  • You want to prettyprint HTML? There are tools for that. Tools that don't consist of a handful of regexes... Commented Dec 5, 2010 at 0:26
  • Let me clarify. If you were to open this file in your average text editor, the output would appear to be prettyprinted. It would not seem like the whole file was a single line. However, when viewed in a browser, because there is no <br> or newline character, everything displays as if it were one huge long line. Commented Dec 5, 2010 at 0:33

1 Answer 1

2

If you want the file to appear on the browser in a way that more closely matches what you see when you open it with a text editor, there are several ways.

Perhaps the easiest is to encase the text in a <pre> block, this way the format is kept as-is.

<pre>
Hello,
this is
a test
</pre>

If you absolutely positively want to add a line break that will "work" with the browser, and you must use Perl, you might try something like this:

perl -n -e "print if s/\n/<br>\n/" < source.html > destination.html

This is pretty awful perl but it works.

You can achieve a similar result with sed:

sed -e "s/$/<br>/" < source.html > destination.html
Sign up to request clarification or add additional context in comments.

Comments

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.