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

"Van Handel" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <000801cd5e38$f21319d0$d6394d70$@net>
Thanks for the answer.  I'll start trying these things.
One thing that seems unclear, in my example I wanted to remove from
the line beginning with
# Purge
but in general, I want to remove the line matching the command pattern specified,
along with any commented lines directly above that.  For example, a cron file
might have some commands and commented lines in it, followed by 5 commented lines,
followed by a line with the specified pattern, followed by some more lines that
could be comments or commands.

I want to remove the matching command line and the 5 commented lines above it,
but as soon as a line above it is not a comment, stop deleting lines.
This one has me confused because I know I need to store commented lines until
I see a non-commented line that contains the specified pattern (which could
possibly mean storing 0 commented lines and the specified command line), and
then delete those lines.  Another idea that might work well with sed is to pipe
the file text through "tac" first, then delete lines from the matched pattern
and beyond until you reach a non-commented line, then pipe it through tac again.

Thanks for the help.


-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Cameron Simpson
Sent: Monday, July 09, 2012 4:44 PM
To: [email protected]
Cc: [email protected]
Subject: Re: How to use sed to remove a line from a cron file and also remove all commented lines directly above it

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.


------------------------------------

--
Yahoo! Groups Links
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.