I'm trying to edit a mod that changes regularly (I'm trying to turn all characters into adults), and in some of the files I need to run a script that finds a specific line like "character = yes" and then find the next iteration of "age = {their age}" and replace it with a random number between 18 and 45.

The real problem I'm having is how to tell the script to look only for the very next instance of string 2 (age) after reading each instance of string 1 (character = yes), then stopping and looking for the next string 1 (character = yes) again.

I don't want to replace all instances of string 2 (age) because it applies to noncharacters as well in the same file. So I only want to replace the very next instance of string 2 (age) that's read after string 1 (character = yes), since it's coded in bracketed blocks.

  • 1
    It would be interesting to see an example of the data before and after modification so that we have something to test with and verify against, and to see the data format. Some structured formats are trivial to work with given the correct tools.
    – Kusalananda
    Jun 21, 2022 at 17:20

1 Answer 1

perl -0777 -pe 's/character = yes.*?age = \K\d+/int(18+rand(45-18+1))/ges' < file

Would replace the first occurrence of age = <digits> after character = yes with age = <some-random-number-between-18-and-45>

You must log in to answer this question.

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