How to make a line in a comment in SED

Sorry if this is a recurrence. I have crontab entries that look like this:

*   *    *    *    *   sleep 15;/etc/opt/wer.sh
1 * * * * /opt/sfm/qwe/as.sh

How to insert # in a line containing a call to "as.sh" using sed ?
How to uncomment it?

+3
source share
3 answers

You can use:

sed '/\/as.sh/s/^/#/'

which will replace the zero-width marker of the starting line with a comment symbol for all lines containing /as.sh, as shown in the following example:

pax> echo '
* * * * * sleep 15;/etc/opt/wer.sh
1 * * * * /opt/sfm/qwe/as.sh
' | sed '/\/as.sh/s/^/#/'

* * * * * sleep 15;/etc/opt/wer.sh
#1 * * * * /opt/sfm/qwe/as.sh

But you need to keep in mind a couple of things.

  • , cron, . , crontab, cron, .
  • . , , , ( ). .

, :

sed '/\/as.sh/s/^#//'

, , /as.sh, # .

+5

:

sed -e "s/\(.*\)\(as.sh\)/\#\1\2/g"

:

sed -e "s/\(^#\)\(.*\)\(as.sh\)/\2\3/g"
+3

crontab -e crontab.

-2

Source: https://habr.com/ru/post/1757280/


All Articles