rylan February 2016

awk string search and replace with appended incremented number

I am having some trouble finding string (topic-link) and then appending an incremented number using awk.

I have the following html file that is all one line:

<a class="topic-link" href="test.com/topic/gastrointestinal">Gastrointestinal</a>, <a class="topic-link" href="test.com/topic/nutrition">Nutrition</a>, <a class="topic-link" href="test.com/topic/weight-gain">Weight Gain</a> </p>

Using awk I am trying to obtain:

<a class="topic-link1" href="test.com/topic/gastrointestinal">Gastrointestinal</a>, <a class="topic-link2" href="test.com/topic/nutrition">Nutrition</a>, <a class="topic-link3" href="test.com/topic/weight-gain">Weight Gain</a> </p>

I am running the following:

awk '{gsub("topic-link","topic-link"++i)}1' input file > output file

The problem is that the results will be as follows:

<a class="topic-link1" href="test.com/topic/gastrointestinal">Gastrointestinal</a>, <a class="topic-link1" href="test.com/topic/nutrition">Nutrition</a>, <a class="topic-link1" href="test.com/topic/weight-gain">Weight Gain</a> </p>

The solution I have come up with will only work correctly if the instances of "topic-link" are on separate lines and as such I am stuck.

Please tell me I am missing something very obvious here or if you have any suggestions for an alternative approach.

Thanks!

Rylan

Answers


Ed Morton February 2016

Your gsub() is only called once so i is only incremented once. You need a loop:

$ awk '{i=0; while(sub(/topic-link"/,"topic-link"++i"\""));} 1' file
<a class="topic-link1" href="test.com/topic/gastrointestinal">Gastrointestinal</a>, <a class="topic-link2" href="test.com/topic/nutrition">Nutrition</a>, <a class="topic-link3" href="test.com/topic/weight-gain">Weight Gain</a> </p>

Post Status

Asked in February 2016
Viewed 3,139 times
Voted 11
Answered 1 times

Search




Leave an answer