Forum Moderators: phranque

Message Too Old, No Replies

How to automatically add <br> after every line break to text files?

         

anon123

10:42 pm on Nov 19, 2009 (gmt 0)

10+ Year Member



Hi,

I have about 100 text files, I want to add a <br> tag to the end of every line break.

This would take me hours to open each file and copy and paste the <br> tag to do it manually.

Does anyone know a quick and easy way to do this?

choster

10:59 pm on Nov 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In a good text editor, search for carriage returns (or carriage return+line feed, depending on the format) and append the <br> or <br />.

In fact, it doesn't even have to be a good text editor; Dreamweaver can do this with Extended Search and Replace :).

anon123

11:18 pm on Nov 19, 2009 (gmt 0)

10+ Year Member



Thanks, but how do you search for carriage returns?

celgins

12:40 am on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Unfortunately, you won't be able to search for, or manipulate carriage returns/line feeds in Notepad. Regular expressions are best dealt with in programs like MetaPad, MS Word, Dreamweaver, or a host of other advanced editors.

If you have a copy of MS Excel, you may want to try importing your TXT files into a single spreadsheet... (with 1 line from your text file being imported as 1 row in your spreadsheet). For example, if your 100 text files have 5 lines each, your spreadsheet would have 500 rows.

Then, add a new column and fill each row of that column with the <br> tag. (Use the Fill > Down feature to do this)

Finally, export this spreadsheet as Text file and you're done.

It will take some time, but not nearly the amount of time it would take to open each TXT, add the <br> tag, and save it.

Oimachi2

1:44 am on Nov 20, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi Anon,

What you need to do this is Notepad++ and create a simple macro, very easy, just record and then play.

Cheers!

[edited by: bill at 4:30 am (utc) on Nov. 20, 2009]
[edit reason] see sticky [/edit]

phranque

5:19 am on Nov 20, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you could certainly do this well with a scripting language such as perl and you could probably do it as well with a generic unix tool such as sed or awk.

g1smd

5:12 pm on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are you sure it is <br> that you need, and not paragraph or list tags?

choster

8:19 pm on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No regex or scripting required.

-> Find (or Extended Search, whatever gives you multi-line input areas) in Folder/Site/Project (however the set of files to be searched is defined in your application)
-> enter a carriage return in the Find field
-> enter <br> in the Replace
-> Replace All

desiguru

3:34 pm on Nov 29, 2009 (gmt 0)

10+ Year Member



Use notepad ++. Select the last word's end of the first line and second line's very first space.

Then hit Ctrl + H and in replace type <br />

[edited by: phranque at 10:41 pm (utc) on Nov. 29, 2009]
[edit reason] No urls, please. See TOS [webmasterworld.com] [/edit]

tangor

6:21 pm on Nov 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



One can also use the <pre></pre> tag to preserve ascii text layouts.

rocknbil

8:52 pm on Nov 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, but how do you search for carriage returns?

This is a cheesy way to do it, but it works.

Get a document open, create a line return.

text

text too

select the VOID between the two lines, it should look something like this, with the pipes being the "selection." Insert your cursor just before the word "text too", as in here -> "¦text too", and drag upward to the blank line.

text
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
text too

CTRL-C. Open your find tool, CTRL-V.

You will have to experiment with it to make sure you're selecting the right thing before cutting it loose on thousands of docs, but it works.