Home > OS >  Find word in text by ID and replace other word in the same line
Find word in text by ID and replace other word in the same line

Time:05-05

I have crontab with some rules. I need to find a line with server ID, and then replace the number of days at the end of the line.

$ crontab -l
00 6 * * * /home/scripts/User/19122391_User_Server_Notification_shutdown_delete.sh 64
00 16 * * * /home/scripts/User/20045967_User_Server_Notification_shutdown_delete.sh 15

ID of server: 19122391 Days: 64

So, I need to change number 64 to another, for example 100 in crontab. To find a line I use this command awk '/19122391/{print}' crontab.bak, but how to replace last number in line, I did not can find solutions.

Can I use bash script to do this?

CodePudding user response:

Using awk

$ awk -i inplace '/19122391/{$NF=100}1' crontab.bak
00 6 * * * /home/scripts/User/19122391_User_Server_Notification_shutdown_delete.sh 100
00 16 * * * /home/scripts/User/20045967_User_Server_Notification_shutdown_delete.sh 15

Using sed

$ sed -i '/19122391/s/[0-9]*$/100/' crontab.bak
00 6 * * * /home/scripts/User/19122391_User_Server_Notification_shutdown_delete.sh 100
00 16 * * * /home/scripts/User/20045967_User_Server_Notification_shutdown_delete.sh 15
  • Related