1

I am trying to use sed to match and replace a string of the following nature somedate:"12/12/2012" using sed 's/somedate:"[0-9]{2}\/[0-9]{2}\/[0-9]{4}"//g'

This expression does not match anything in the string something and somedate:"12/12/2012" and something else

What am I doing incorrectly?

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
cer_albastru
  • 215
  • 3
  • 13
  • Does this answer your question? [Using different delimiters in sed commands and range addresses](https://stackoverflow.com/questions/5864146/using-different-delimiters-in-sed-commands-and-range-addresses) – tripleee Feb 17 '21 at 07:02

1 Answers1

3

A few points:

  • You use / as the delimiter for sed but the substitution contains / so choose a different delimiter such as | or escape the / in the substitution with \. Any delimiter can used with sed.

  • The quantifier {n} is part of the extended regexp class so use the -r option (or -E for BSD derivatives of sed) or again escape the extended features like \{2\}.

  • The g flag may or may not be needed depending if you have multiple matches on a single line. It doesn't make a difference for your given example but it's worth pointing out.

  • You probably want {1,2} for the days and months i.e 1/1/2012.

I would do:

$ sed -r 's|somedate:"[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}"||' file
something and  and something else

Alternatively by escaping everything:

$ sed 's/somedate:"[0-9]\{1,2\}\/[0-9]\{1,2\}\/[0-9]\{4\}"//' file
something and  and something else
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
  • I did not know that one could use any delimiter with sed --so Thanks for that pointer. However, neither of these expressions match 'somedate:' in the example string. – cer_albastru Mar 05 '13 at 15:49
  • @cer_albastru actually they both do see http://ideone.com/xcYYqD you must be doing something different. – Chris Seymour Mar 05 '13 at 15:52
  • I am using cygwin and here is what I tried: echo 'something and somedate:"10/3/2011" and anotherdate:"1/31/2012" and (one or two )' | sed -r 's|somedate:"[0-9]{2}/[0-9]{2}/[0-9]{4}"||' --any ideas? – cer_albastru Mar 05 '13 at 15:58
  • Haha look at the date in that test string it's only one digit, not two `:]` You want `sed -r 's|somedate:"[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}"||'`. – Chris Seymour Mar 05 '13 at 16:02