calvinkrishy
calvinkrishy

Reputation: 3928

Delete using a different delimiter with Sed

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?

Upvotes: 44

Views: 13261

Answers (4)

Stobor
Stobor

Reputation: 45132

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

Upvotes: 58

ghostdog74
ghostdog74

Reputation: 342649

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"

Upvotes: -1

ghostdog74
ghostdog74

Reputation: 342649

use gawk

gawk '!/timebomb/' file > newfile

Upvotes: 0

Jonathan Feinberg
Jonathan Feinberg

Reputation: 45344

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.

Upvotes: 1

Related Questions