LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Help with regexp (https://www.linuxquestions.org/questions/linux-software-2/help-with-regexp-625858/)

anupamsr 03-05-2008 05:52 AM

Help with regexp
 
Hi!

I have a html file with following data in it.

<h3>Some title</h3><a name="something"></a>
March 13
...
<h3>Some other title</h3><a name="something"></a>
Feb 21
...

I want to replace such instances with

<h3>Some title</h3><a name="March 13"></a>
...
<h3>Some other title</h3><a name="Feb 21"></a>
...

Is it possible? Perl/Pything/Shell anything is good. Shell is preferred.

AnanthaP 03-05-2008 06:03 AM

A pseudo code:
(1) If line begins with <h3>, then store it.
(2) If previous line was <h3>, then store the date, and rewrite the <h3> line with changed url, and also write the current line.
(3) else write the line.

btw. whats the use?

End

anupamsr 03-05-2008 06:11 AM

To create a table of blog entries and uniquely name-reference them :)
The date is actually full "date -Ru" so for all practical purposes, it will be unique...

anupamsr 03-05-2008 06:19 AM

Could you please tell me how to do it in perl? I don't remember it at all!

anupamsr 03-05-2008 06:43 AM

This does the trick for the first time.
Code:

cat blog.html|sed ':a;N;$!ba;s/<\/h3>\n[ \t]*/<\/h3>/g'|perl -p -e "s,<h3>.*</h3>,<a name=\"$'\"></a>$&\n,g"
For a script that I can run on the generated file, I will have to dig into your algorithm :)


All times are GMT -5. The time now is 10:48 AM.