sed adding/removing comment in crontab


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed adding/removing comment in crontab
# 1  
Old 11-08-2010
sed adding/removing comment in crontab

I have a requirement where I want to add a comment '#' in my crontab, run a process, than remove the '#' I added.


Example cron

#5,10 * * * * ls -lt /tmp
10,5 * * * * ls -lt /var

I would like to be able use sed or awk to add a '#' at the begining of each
line. After the command my crontab should look like this.

##5,10 * * * * ls -lt /tmp
#10,5 * * * * ls -lt /var

Though I triied this on a regular file I believe I know how to remove only the first occurance '#' ONLY, which would leave me with my original cron

Code:
 sed -e 's/#//' | crontab

#5,10 * * * * ls -lt /tmp
10,5 * * * * ls -lt /var
# 2  
Old 11-08-2010
IMO:
Comment: aagh! don't do that. No sed. No awk.

Use the crontab command only. Some people have used vi, but editor settings have the potential to render your whole crontab into garbage.

You can do what you want, of course, but if you keep on doing what you seem to be doing, keep lots of backups. And remember, if this is the root crontab you can break lots of other things accidentally.

try
Code:
crontab -l | [sed expression here]

to see what you are getting.
# 3  
Old 11-08-2010
Quote:
Originally Posted by jim mcnamara
IMO:
Comment: aagh! don't do that. No sed. No awk.

Use the crontab command only. Some people have used vi, but editor settings have the potential to render your whole crontab into garbage.

You can do what you want, of course, but if you keep on doing what you seem to be doing, keep lots of backups. And remember, if this is the root crontab you can break lots of other things accidentally.

Quote thanks for the tip. Not sure why you see this as a problem, can
you elaborate a bit more?

My issue is I want to automate a process through a script and I want
the cron disabled while the script is running. Would this be a better solution?

crontab -l > filename # save cron file
crontab -r # remove cron file
do work
....
....
crontab <file name> # re-instantiate cron
# 4  
Old 11-08-2010
You save a cron file, lets call it cronfile
you do the changes to the file...
you load the new file:
Code:
crontab cronfile

Dont forget to be the correct user for the correct cron file you wish to modify/load

(no need to stop cron nore remove...)
# 5  
Old 11-08-2010
Thanks for the follow up.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding a comment in a file next to data

Dear Gurus, I have a file which comes every day with set of data, as a part of processing i want to add a comment at the start of every line. e.g of file <PCL> 2E;"HCA";"COP Car A";"ODBS_CFG" 7C;"DD";"Doors Car D";"ODBS_CFG" 3D;"XA";"Auxiliary Car A";"ODBS_CFG" 3E;"XB";"Auxiliary... (12 Replies)
Discussion started by: guddu_12
12 Replies

2. AIX

Comment out crontab using sed command

I am trying to comment out the crontab entries using sed. I want to comment it out for a particular environment say '/mypath/scripts/'. Using the full path as pattern, it is working. but using variable it is not working. i have tried double quotes too. but no luck! $ crontab -l ... (3 Replies)
Discussion started by: SKhan
3 Replies

3. Shell Programming and Scripting

Removing the sas comment line using UNIX

I have tried a lot, Need your help guys. SAS Program: data one ; /* Data step */ Input name $; /*Dec variables*/ I want to remove the commented part(/* Data step */) alone. I have tried using sed command but it is deleting the entire line itself. i need unix command to separate this and... (6 Replies)
Discussion started by: saaisiva
6 Replies

4. Shell Programming and Scripting

sed/awk script to replace only FIRST comment in the file

My first comment on every file contains the license message. I want to replace with a new license message. I used the below sed script, which replaces all comments. What is the modification or any other method with awk script for the below to edit only the first comment(license message)? #sed -f... (1 Reply)
Discussion started by: vpshastry
1 Replies

5. Shell Programming and Scripting

Comment a line with SED

I have around 25 hosts and each hosts has 4 instance of jboss and 4 different ip attached to it . I need to make some changes to the startup scripts. Any tips appreciated. I have total of 100 instances which bind to 100 different ip address based on instance name. For example File1 ... (1 Reply)
Discussion started by: gubbu
1 Replies

6. Shell Programming and Scripting

best way for removing comment from shell scripts -- bash

Again a comment removal requirement from me, refer my previous problem & solution for removing comment from ruby scripts: https://www.unix.com/shell-programming-scripting/118296-best-way-removing-comment-ruby-program.html This time, it is for stripping of comments from Shell Script. I search for... (2 Replies)
Discussion started by: thegeek
2 Replies

7. Shell Programming and Scripting

best way for removing comment from ruby program

Hi all, I would want to remove all comments from my ruby/rails program. It may seem like a simple task, but it is not so. Because you need to have your tool implemented as like your language parser which is actually not so easy. And am in the search of it, to remove comment from ruby/rails.... (4 Replies)
Discussion started by: thegeek
4 Replies

8. Shell Programming and Scripting

Using sed to comment out line in /etc/vfstab

I am running a script remotely to do the following 1. Kill all processes by a user 2. Uninstall certain packages 3. FTP over a new file 4. Kill a ldap process that is not allowing my /devdsk/c0t0d0s7 slice to un-mount 5. Unmount /h 6. comment out the slice in vfstab 7. newfs the... (9 Replies)
Discussion started by: deaconf19
9 Replies

9. Shell Programming and Scripting

Help using SED to comment XML elements

I'm trying to write a script to help automate some VERY tedious manual tasks. I have groups of fairly large XML files (~3mb+) that I need to edit. I need to look through the files and parse the XML looking for a certain flag contained in a field. If I find this flag (an integer value) I need... (4 Replies)
Discussion started by: J-Hon
4 Replies

10. Shell Programming and Scripting

get rid of xml comment by grep or sed

Hi, I would like to get rid of all comment in an xml file by grep or sed command: The content seem like this: <!-- ab cd ef gh ij kl --> Anyone can help? Thanks and Regards (3 Replies)
Discussion started by: RonLii
3 Replies
Login or Register to Ask a Question