mila002 mila002 - 1 month ago 13
Linux Question

Using sed to replace text with spaces and slashes

I'm trying to replace an entry in Crontab (RedHat) with sign #.
I tried sed command like this

sed -i 's|35 15 * * * /tmp/vii/test.sh >/dev/null 2>&1|#|g' /var/spool/cron/root


but it doesn't work. Any ideas?

Answer

Don't try to edit crontab files directly, use crontab tools instead!!

You could use:

crontab -l

to dump actual crontab list.

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

... or more finely:

crontab -l | sed '/^35 \+15.*vii.test.sh/s/^/# /'

to pre-edit crontab list and see on terminal sed command result

Then:

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

to replace actual crontab list, once everything are ok.

But warn!

wrong Command | crontab

will erase completely your crontab list!!