To increase performance in FORTRAN code, I would like to rearrange the indexes of arrays so that the 4th index is moved to second place, for example, I want to change the next line
ts(l,i,j,k) = ts(l,i,j,k1(i,j))
to
ts(l,k,i,j) = ts(l,k1(i,j),i,j)
Note that this is just an example of a string, indexes are not always called i, j, k, l ... I just know the name and rank of the array. Therefore, I cannot just separate 4 arguments from commas, since a single argument can also be a matrix having a comma (in the above case, k1 (i, j)). So my first idea
sed -r 's/ts\(([^,]+),([^,]+),([^)]+),([^,]+)\)/ts\(\1,\4,\2,\3\)/g' *.F
fails in this case (rhs in the above line of code) because it gives:
ts(l,k,i,j) = ts(l,j),i,j,k1(i)
I need this regular expression that splits my array indices only when a maximum of 1 bracket is open. Can someone give me a hint how to do this with sed / python / perl?
best wishes