Linux Mastering Development

Editing a file based on a pattern appearance

I have a file that contains text and numbers like this:

state(1, s(1,a), [s(1,b)]).
state(1, s(1,b), [s(1,a)]).
state(1, s(2,a), [s(2,b)]).
state(1, s(2,b), [s(2,a)]).
state(1, s(3,a), [s(3,b)]).
state(1, s(3,b), [s(3,a)]).
state(1, s(4,a), [t(1), t(2)]).
state(1, s(5,a), [t(1), t(3)]).
state(1, s(6,a), [s(6,b)]).
state(1, s(6,b), [s(6,a)]). on

I want the numbering with in s(x,y) to change as follows:

state(1, p(1), 0, [p(2)]).
state(1, p(2), 0, [p(1)]).
state(1, p(3), 0, [p(4)]).
state(1, p(4), 0, [p(3)]).
state(1, p(5), 0, [p(6)]).
state(1, p(6), 0, [p(5)]).
state(1, p(7), -1, [t(1), t(2)]).
state(1, p(8), -1, [t(1), t(3)]).
state(1, p(9), 0, [p(10)]).
state(1, p(10), 0, [p(9)]). on

The 0 and -1 in the third argument in the state are just to suggest that there was/is an s-s (or p-p) connection(represented as 0), or not (represented as -1).

I tried "sed", but I was not at all near successful.

Can I get some hints or solutions?

Leave a Reply

Your email address will not be published. Required fields are marked *