Find a text and if condition matches then replace it

Need a script that can find text in a file and replace it accordingly.
This is the file I have:

while IFS=',' read -r f1 f2 f3
do
{
nohup /home/testuser/dbmaintenance/sys_offline_maintenance.sh $f1 $f2 $f3 > $f2.out &
}
done < "/home/testuser/dbmaintenance/week1offlineserver.txt"

In this file I need a script that can look for the number after week in week1offlineserver.txt and replace it like: If number is 1, change it to 2, if number is 2 then to 3 and if it is 3 then to 4 and if it is 4 then to 1.

I question the wisdom of what you are trying to do, but I will not elaborate unless you care to share.

Please, try the following:

 perl -pe 'BEGIN{%id=(1=>2, 2=>3, 3=>4, 4=>1)} s/week(\d)off/week$id{$1}off/g' program.sh

if you like the result run it as this to save it to the same file.

perl -i -pe 'BEGIN{%id=(1=>2, 2=>3, 3=>4, 4=>1)} s/week(\d)off/week$id{$1}off/g' program.sh
1 Like

We need to run maintenance on servers listed in the text files. I need to be able to have the file automatically change the text file (to a new list of servers) ever week.
The servers are in week1..., week2... and so on.
So basically my aim is to have the server rotate the list of servers going formaintenance every week.

------ Post updated at 07:49 PM ------

perl -i -pe 'BEGIN{%id=(1=>2, 2=>3, 3=>4, 4=>1)} s/week(\d)off/week$id{$1}off/g' program.sh

What is program.sh here?

The place holder for whatever you script is named, the one you want to re-write.

1 Like

Many Thanks! Works like a charm.