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
  • 2
    Let's hope it's not HTML tags: stackoverflow.com/questions/1732348/… :)
    – user1931
    Commented Feb 1, 2010 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 Commented Feb 1, 2010 at 20:37

3 Answers 3

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"}
4
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

1
  • 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. Commented Feb 8, 2010 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.

1
  • apparently, not true. Commented Feb 1, 2010 at 21:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .