2

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?

HatLess
  • 5,048
  • 4
  • 8
  • 28
Oleg
  • 141
  • 7

1 Answers1

3

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
HatLess
  • 5,048
  • 4
  • 8
  • 28
  • Thank you, but after this command my crontab does not have changes. Main goal is to change number in file. – Oleg May 04 '22 at 10:19
  • @Oleg use inplace edit. I will update answer to include it – HatLess May 04 '22 at 10:21
  • 1
    GNU awk and inplace edit, OB. link: https://stackoverflow.com/a/16531920/4162356 – James Brown May 04 '22 at 10:36
  • @HatLess Thank you, it works!!! :) And can I use this command also with crontab, not with test file crontab.bak? – Oleg May 04 '22 at 10:47
  • @Oleg You can use the specific user cronfile found under `/var/spool/cron/crontabs/` – HatLess May 04 '22 at 10:49
  • 1
    @HatLess Cool!!! Thank you very much! And one last moment, could you please explain, what does it mean `1` in command `awk -i inplace '/19122391/{$NF=100}1'` – Oleg May 04 '22 at 10:52
  • @Oleg It is a shorthand to the `{print}` command. – HatLess May 04 '22 at 10:58
  • @HatLess Hello, could you please tell me, Is it possible using this command to plus amount of days. For example, if I have 64 days and I need more 10 days, can I make in command to find 64 days and make plus 10 days? – Oleg May 09 '22 at 10:19