4

why this does not match..?

sed -e '/--Updated?[[:space:]]+Date/d' inputfile

this..:

--Updated Date: 2013-11-06 15:32:13

d? is because sometimes I have Update Date, and sometimes Updated Date.

for removal? I have tried with \s too, not working.

3 Answers 3

7

you need to use the -r parameter. try use

sed -r '/--Updated?[[:space:]]+Date/d' inputfile

updating answer

When you use sed '/something/d' , every line that match with this will be deleted.

-r - the parameter -r is use extended regular expressions .

Inside the expression have 2 regular expressions.

[[:space:]] - Match with all whitespace characters, including line breaks

? - optional

+ - one or more times.

SO, the command sed will delete every line that match with --updated and than one or more whitespace character and than Date but because of the ? the character d is optional. like:

--Updated Date: 2013-11-06 15:32:13
--Updated   Date: 2013-11-06 15:32:13
--Updated           Date: 2013-11-06 15:32:13
--Update Date: 2013-11-06 15:32:13
2
  • yes that was it.
    – branquito
    Commented Mar 28, 2014 at 15:18
  • Please explain your command.It would be helpful for noobs like me. Commented Mar 28, 2014 at 16:00
5

To do this using POSIX basic regex, the closest I can come is:

sed '/--Updated*[[:space:]][[:space:]]*Date/d' inputfile

Unfortunately there is no real substitute for ?, so a * is used which would also match multiple ds. The + however can be replaced by simply repeating the pattern an using a * for the second repetition.

Update

Actually the way to get the equivalent effect of the extended expressions is to use two patterns:

sed \'
  /--Updated[[:space:]][[:space:]]*Date/d
  /--Update[[:space:]][[:space:]]*Date/d
  ' inputfile
10
  • Is there any real benefit using this, or I am good with -r? I am using GNU sed, so I should be safe?
    – branquito
    Commented Mar 28, 2014 at 15:32
  • 1
    @branquito, if you are sure that GNU sed will always be used, then you will be fine to use the extended expressions. If you will run it on another system, eg on OSX, then the -r option may not be in the default sed.
    – Graeme
    Commented Mar 28, 2014 at 15:37
  • I think probably [d ]* would be a little more explicit, if not by much. It is definitely tricky.
    – mikeserv
    Commented Mar 29, 2014 at 0:32
  • @mikeserv, updated, just realised that the best way is just to use two patterns.
    – Graeme
    Commented Mar 29, 2014 at 0:50
  • This is true, but performance-wise ( if it can be counted in this case ) it does have a negative effect as sed now has two directives to check per line. Generally though, at least in my experience this is a non-issue, as sed is probably the fastest solution in any case that involves editing. Grep might be faster in this case though if you use -v. The only times i've been disappointed with sed is when ive had a choice between using it with two outs and the w directive or tee and two concurrent greps. The latter seems always to win out.
    – mikeserv
    Commented Mar 29, 2014 at 0:56
2

With gnu sed 4.2.2 on cygwin, add the -r flag:

 '--Updated Date: 2013-11-06 15:32:13'|sed -r -e '/--Updated?[[:space:]]+Date/d'

prints no output. -r turns on extended regular expressions so ? and + will work like you expect. The reference for extended regular expressions I use most points out that ? and + have to have a leading backslash in basic regular expressions (the default) in order to have their special meaning.

1
  • I use the same reference, it's excellent.
    – branquito
    Commented Mar 28, 2014 at 15:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .