0

I want to write a script that finds a open/close tag pair in a text file and prepends a fixed string to each line between the pair. I figure I use grep to find the tag line numbers and either awk or sed to place the tags, however, I'm not sure how exactly to do it.

Can someone help?

  • 2
    Let's hope it's not HTML tags: stackoverflow.com/questions/1732348/… :) – John T Feb 1 '10 at 20:34
  • I'm thinking about something like if you put ;tag; <text> ;tag; then my program will go through and prepend tag to every line of <text>. I'm working on a note taking tool using markdown, but it's a pain to manually put a > on every line, or even > > :-p – Ritwik Bose Feb 1 '10 at 20:37
1

In awk:

START                  {noprefix="true"}
/<close tag regex>/    {noprefix="true"}
noprefix=="false"      {print "prefix", $0}
noprefix=="true"       {print $0}
/<open tag regex>/     {noprefix="false"}
1

It should be done by one of the traditionally syntax aware languages (yacc etc). Doing it with grep and the like may be okay for specific cases but regexp simply is not powerful enough to catch the subtleties of HTML

  • actually, I got it to work just fine. I wrote a over awk in bash and stored the awk scripts as a text file. But it does everything I need it to do. – Ritwik Bose Feb 8 '10 at 14:34
0

You should consider using yacc for it. It is NOT possible to do this with sed, awk or grep without a considerable amount of effort. As for learning yacc, it wouldn't take more time than it did for learning sed/awk/grep. And it will be really easy that way.

  • apparently, not true. – Ritwik Bose Feb 1 '10 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.