I need to insert a block of code (in fact it is an adsense ads script) between two specific tags (html file) that are the following:
</style>
<table border="1" class="dataframe">
I need to insert it in the THIRD occurrence of these two labels. The form of a typical Adsense block is:
<script async
src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
</script>
<script>
lorem ipsum...
</script>
In the end I need to have something like this:
</style>
<script async
src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js">
</script>
<script>
lorem ipsum...
</script>
<table border="1" class="dataframe">
I have inserted blocks of code using sed and indicating the line number, in this case it is not possible in this way because the line number can change.
Thank you very much for any help.
awk '/pattern to match/{match_count++} {if (match_count==3) {print "your extraStuff"}1' htmlfile > new.htmlfilewill give you something to experiment and search further on. The1after the closing}indicates 'print all input". Remove that to experiment. You can add explicit instructions on when to print input, using more{if{...}else{}}logic. Spend a few hours with the awk tutorial and you'll be on your way toawkguruhood ;-) Good luck.htmloutput.htmlshould really only be parsed with anhtmlaware parser. Once and element breaks across lines,awkwill cry fowl, as it is a line based parser, not an<tag> ...</tag>.. parser. Good luck.