mila002 mila002 - 1 year ago 84
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/ >/dev/null 2>&1|#|g' /var/spool/cron/root

but it doesn't work. Any ideas?

Answer Source

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 '/^/# /'

... or more finely:

crontab -l | sed '/^35 \+15.*^/# /'

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


crontab -l | sed '/^/# /' | crontab

to replace actual crontab list, once everything are ok.

But warn!

wrong Command | crontab

will erase completely your crontab list!!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download