tags:

views:

346

answers:

4

Fairly certain 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?

Thanks!

+5  A: 

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'.)

Stobor
+1  A: 

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
A: 

use gawk

gawk '!/timebomb/' file > newfile
ghostdog74
A: 

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"