Commenting contab from a script

Dear All,
I have many cron entries and want to comment a particular cron entry from a script.
Ex- Suppose I have the below cron entries:

# DO NOT EDIT THIS FILE - edit the master and reinstall.
#Cron entries for Jboss server 1
0 23 * * * /usr/bin/echo
0 23 * * * /usr/bin/asdg_count.sh
0 23 * * * /usr/bin/true
0 23 * * * /usr/bin/true

I want to comment the entry for asdg_count.sh from my script
I am using the below command:

(echo ^[:g/watchdog/s/^/#/^[:wq!^M)|crontab -e >> temp.txt

But the command is not working
and I am getting the console message as :

Vim: Warning: Output is not to a terminal
Vim: Warning: Input is not from a terminal
crontab: "vi" exited with status 1

and the message in temp.txt is :

-- Ip/crontab.XXXXOKeTQL" 7L, 305C
Vim: Error reading input, exiting...
Vim: preserving files...
Vim: Finished.

Please help on commenting the cron from the script.

Thanks
AK

One way which may help you.,

  1. Backup the crontab as

crontab -l >> tmpfile.txt

  1. Use sed with -i option to substitute.

  2. and then remove all crontab.

crontab -r

  1. install the new file as

crontab tmpfile.txt

It will work best for you.... instead of trying to do the edition with vim.
Also, note that, if some body else edits that crontab at the same time then you will end up in mess..

crontab -l | sed '/asdg_count.sh/ s/^/#/' | crontab -

crontab -e | sed '/watchdog/ s/^/#/ | crontab -

I guess its not complete ...

please help

crontab -e, complete or not, is wrong!

crontab -l was mentioned, though.

I would be cautious of automating anything with the crontab file without taking a backup first.

crontab -l | tee crontab.bak | sed '/asdg_count.sh/ s/^/#/' | crontab -

avishek007 the example you post is looking for a line in crontab containing the word "watchdog" not "asdg_count.sh".

There is a minor typo in cfajohnson's post (missing quote) which has been carried forward into the next two posts. Perhaps that is the problem?

crontab -l | sed '/asdg_count.sh/ s/^/#/' | crontab -

I would always advise copying crontab to a safe area before editing the file. The rest really depends on local rules.