How do I grep with sed and substitute a "#" on that line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I grep with sed and substitute a "#" on that line
# 1  
Old 07-14-2010
How do I grep with sed and substitute a "#" on that line

Hey all,

I am trying to disable a certain cronjob before I run a backup. I want to be able to add/remove a "#" from the beginning on the crontab line it is located on.

Here is the crontab:

Code:
46 11 * * * /etc/webmin/cron/tempdelete.pl
@daily /etc/webmin/time/sync.pl
*/5 * * * * /opt/watchdog/startupWatchdog.sh

Does anyone know how to grep the "watchdog" line and add a "#" to it. Also to go backwards have a line that removes the "#" from that line so I can enable it after the backup.



Here is what I am working with at the moment:

Code:
#!/bin/bash

crontab -l >/tmp/crontab.a

sed -e 's/grep watchdog here?/#/' /tmp/crontab.a

rm /tmp/crontab.txt

Smilie

Any help is appreciated!

Thank You
# 2  
Old 07-14-2010
Code:
sed '/Watchdog/s/^/#/' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 07-15-2010
-----------------------------------------------------------------------------
UPDATE
-----------------------------------------------------------------------------

Here is the original crontab -l

Code:
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXXlCjhoF installed on Thu Jul 15 14:17:25 2010)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
USER=root
JAVA_HOME=/usr/java/latest




46 11 * * * /etc/webmin/cron/tempdelete.pl
@daily /etc/webmin/time/sync.pl
*/5 * * * * /opt/watchdog/sstartupWatchdog.sh

My code removes the top 3 lines that are duplicated in script each time, then it comments out the "watchdog" line to disable the cronjob. I have a couple issues, one is that my script is messy:

Code:
crontab -l > /tmp/crontab.a
grep -v "DO NOT EDIT" /tmp/crontab.a > /tmp/crontab.b
grep -v "installed on" /tmp/crontab.b > /tmp/crontab.a
grep -v "(Cron version" /tmp/crontab.a > /tmp/crontab.b
sed -e '/Watchdog/s/#//' /tmp/crontab.b > /tmp/crontab.a # Disable Watchdog
crontab /tmp/crontab.a
rm /tmp/crontab.a /tmp/crontab.b

My other issue is if the "watchdog" line is already commented out then this script adds an aditional comment to the line giving me:

##*/5 * * * * /opt/watchdog/sstartupWatchdog.sh

If I was to run the enable script it will just delete one of the comments.


Please let me know if this makes any since.

Thanks!
Mike
# 4  
Old 07-15-2010
you can rewrite the sed command as: sed '/Watchdog/s/^[#]*//' /tmp/crontab.a.
# 5  
Old 07-15-2010
disable the cronjob:

Code:
sed '/Watchdog/s/^/#/' myFile

enable the cronjob:

Code:
sed '/Watchdog/s/^#//' myFile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Failure: if grep "$Var" "$line" inside while read line loop

Hi everybody, I am new at Unix/Bourne shell scripting and with my youngest experiences, I will not become very old with it :o My code: #!/bin/sh set -e set -u export IFS= optl="Optl" LOCSTORCLI="/opt/lsi/storcli/storcli" ($LOCSTORCLI /c0 /vall show | grep RAID | cut -d " "... (5 Replies)
Discussion started by: Subsonic66
5 Replies

2. Shell Programming and Scripting

In Vi "sed" substitute word on a specific line

i need to substitute word on a specific line. I was able to do it on command line like below but it is not working in vi. command line like below: sed -e '8s/table_name/schema.table_name/' file_name. in vi table_name and schema are my positional parameters that i pass into the script. ... (5 Replies)
Discussion started by: pimmit22043
5 Replies

3. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

6. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

7. Shell Programming and Scripting

How to substitute "\" by "\/" using SED?

Input: a/b/c Output required: a\/b\/c This does not work: sed s/'\/'/'\//'/g (6 Replies)
Discussion started by: indianjassi
6 Replies

8. Shell Programming and Scripting

Can "sed" substitute word on a specific line?

Hello experts, I know line number of the word I want to replace. Can "sed" substitute word on a specific line? As well, can sed substitute words inside a specific patten. ex. <word>lalala</word> #replace anything between <word> and </word> minifish (2 Replies)
Discussion started by: minifish
2 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. Shell Programming and Scripting

sed substitute / for \ : error "Function can not be parsed"

Hello all, I have a weird issue when trying to substitute the slashes into backslashes. If I execute this on the command-line (bash / ksh) I get the path correctly translated with backslashes instead of slashes. > echo $PWD | sed 's/\//\\/g' However, when I put this in my script to... (3 Replies)
Discussion started by: candyflip2000
3 Replies
Login or Register to Ask a Question