Re: How to use sed to remove a line from a cron file and also remove all commented lines directly above it

Cameron Simpson <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
On 09Jul2012 21:30, [email protected] <[email protected]> wrote:
| I realize you can't just run sed on a cron file, but I need to run sed to 

If you're root you probably can, though you'd need to restart cron to
make it see the changes.

| delete the command and all commented lines directly above the removed line. 
| 
| Basically, for any text file like below, remove the line containing the pattern 
| 
| and any commented lines directly above it. 
| 
| In the text below, I want to remove every line from the line beginning with Purge 
| 
| through the line containing the pattern $BACKUPDIR/runBackup.sh . 
| 
| Any ideas? 

Do it in two steps. First, write the sed script and test it against a
copy of the crontab. Make the sed script a standalone file in its own
right i.e. not just "sed -e 'blah'" on the command line but "sed -f
sed_script.txt". Personally, I would be commenting out the lines
instead of removing them because my paranoia is great. Eg, put:

  ## autoremoved - YYYYMMDD ## 

on the front of the lines. Next time someone hand edits the file they
can go for real with a human's eye on them.

Second, invoke the "crontab -e" command with editor set to

  sed -i -f sed_script.txt

i.e.

  EDITOR='sed -i -f sed_script.txt' crontab -e -u username

Regarding the script itself, I suppose something like this (totally
untested):

  /^# Purge/,/^[^#].*runAnotherCmd.sh/s/^/## autoremoved - YYYYMMDD ##/

Obviously, adjust to suit. (And "d" instead of "s///" if you're you and
not me.)

If your system's sed does not have the -i option you can used "bsed" instead
of "sed -i":

  https://bitbucket.org/cameron_simpson/css/src/tip/bin/bsed

Cheers,
-- 
Cameron Simpson <[email protected]>

          "Neomort" : Brain-dead human, kept alive for medical purposes.
"Medicine Lecturer" : Brain-dead human, kept alive for medical purposes.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.