Startseite > Uncategorized > Creating an ordered list with sed for HTML

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:

1.

2.

3.

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:

<li>

<li>

<li>

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  > $i.new; 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 “.txt.new”. 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

About these ads
  1. Es gibt noch keine Kommentare.
  1. No trackbacks yet.

Kommentar verfassen

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

WordPress.com-Logo

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

Twitter-Bild

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

Facebook-Foto

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

Google+ photo

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

Verbinde mit %s

Folgen

Erhalte jeden neuen Beitrag in deinen Posteingang.

Schließe dich 122 Followern an