Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 3129  / 2 Years ago, fri, april 15, 2022, 8:27:21

I have text file similar to this:



Some-text
Keyword
Some-text
Some-text
Keyword
Some-text
Keyword
Keyword


I want to replace every "Keyword" with "number.extension" where number is 1 at first keyword match, 2 at second keyword match etc.



I know that I should use bash loop combined with sed (sed 's:Keyword:Number:g'), but I am not sure how to insert variable for number in that sed command.



As this is HTML file, there can be blank lines or any text beetween those "Keywords".


More From » bash

 Answers
6

Does this do what you want?



perl -pe '$n++ if /Some-text/;s/Keyword/SomeText '''$n'''/'


For more complicated replacements (e.g. where maths is involved), more advanced scripting language might be more suitable.


[#32288] Sunday, April 17, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arsleyeuth

Total Points: 72
Total Questions: 121
Total Answers: 112

Location: North Korea
Member since Mon, Oct 31, 2022
2 Years ago
;