Awk Learning

Questions

How to markup text for a particular delimiter, when its contents can span lines? (Example thanks to catonmat)

input:

Yesterday I was walking in =the street=, when I saw =a
black dog=. There was also =a cat= hidden around there. =The sun= was shining, and =the sky= was blue.
I entered =the
music
shop= and I bought two CDs. Then I went to =the cinema= and watched =a very nice movie=.
End of the story.

desired output:

Yesterday I was walking in <tag>the street</tag>, when I saw <tag>a
black dog</tag>. There was also <tag>a cat</tag> hidden around there. <tag>The sun</tag> was shining, and <tag>the sky</tag> was blue.
I entered <tag>the
music
shop</tag> and I bought two CDs. Then I went to <tag>the cinema</tag> and watched <tag>a very nice movie</tag>.
End of the story.

I can do this in Emacs with query-replace-regexp: = to \,(if (evenp \#) "<tag>" "</tag>").

With Awk, if I try to use:

awk -v RS='=' 'ORS=NR%2?"<tag>":"</tag>"' markup-file.txt

Then I get a trailing <tag> because it outputs an ORS after the last record (from the last = to EOF).

End of the story.
<tag>

How can I avoid that?

Created: August 26, 2016
Last modified: September 28, 2019
Status: in-progress notes
Tags: notes, awk

comments powered by Disqus