Creating an ordered list with sed for HTML

I am no sed (streaming editor) crack – and  use it only a few times a year. But as I liked to reform an ordered list from text to HTML I had the wish to use it again. The ordered list was formatted in this way:




To reformat I saved the data to a file named “old” and then opened a terminal. With a little old knowledge and some try and error I first tried with a dry run how sed would reformat:

sed s/^[0-9].\./\<li\>/g raw

And the result was:




For starters it is important that you should not write sed output to the input file, but rather to a new file:

sed s/^[0-9].\./\<li\>/g raw  > new

I wont go into detail into what all those so-called regular expressions mean. And you can read a lot about how sed works n the internet. My point was only to give one practical example of a powerful but small tool. If one uses sed in shell scripts it is even more powerful. You can for instance use something like a command line :

for i in *.txt; do sed s/^[0-9].\./\/g $i  > $; done

to go through all files ending with “.txt” in a specific directory and fill the variable “$i”. And then each file which has been worked on gets a second one which is reformatted and ends with “”. You then can copy or move these files to the old locations if you are satisfied with the results with another for loop.

I got never to the point to perfect my regular expression skills. I am still mostly play around with it. Which is really easy with sed. But be aware, that regular expressions differ in many programs

Creating an ordered list with sed for HTML

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

Du kommentierst mit Deinem Abmelden / Ändern )


Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )


Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s