41

Fairly certain I am missing something obvious!

$ cat test.txt
  00 30 * * * /opt/timebomb.sh >> timebomb.log
  01 30 * * * /opt/reincarnate.sh >> reincarnation.log
$ sed ':timebomb:d' test.txt
  00 30 * * * /opt/timebomb.sh >> timebomb.log
  01 30 * * * /opt/reincarnate.sh >> reincarnation.log

whereas

$ sed '/timebomb/d' test.txt
  01 30 * * * /opt/reincarnate.sh >> reincarnation.log

Why is it the case? Aren't a different set of delimiters supported for the d command?

jww
  • 97,681
  • 90
  • 411
  • 885
calvinkrishy
  • 3,798
  • 8
  • 30
  • 45
  • Possible duplicate of [Use slashes in sed replace](http://stackoverflow.com/questions/5864146/use-slashes-in-sed-replace) – tripleee Dec 17 '15 at 12:28

4 Answers4

56

The delimiters // that you're using are not for the d command, they're for the addressing. I think you're comparing them to the slashes in the s/// command... However although both relate to regular expressions, they are different contexts.

The address (which appears before the command) filters which lines the command is applied to... The options (which appear after the command) are used to control the replacement applied.

If you want to use different delimiters for a regular expression in the address, you need to start the address with a backslash:

$ sed '\:timebomb:d' test.txt
  01 30 * * * /opt/reincarnate.sh >> reincarnation.log

(To understand the difference between the address and the options, consider the output of the command:

$ sed '/timebomb/s/log/txt/' test.txt

The address chooses only the line(s) containing the regular expression timebomb, but the options tell the s command to replace the regular expression log with txt.)

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Stobor
  • 44,246
  • 6
  • 66
  • 69
1

The colon preceeds a label in sed, so your sed program looks like a couple of labels with nothing happening. The parser can see colons as delimiters if preceded by the s command, so that's a special case.

Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
0

use gawk

gawk '!/timebomb/' file > newfile
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
0

you can just use the shell, no need external tools

while read -r line
do 
    case "$line" in
        *timebomb* ) continue;;
        *) echo "$line";;
    esac        
done <"file"
ghostdog74
  • 327,991
  • 56
  • 259
  • 343