1

I cannot seem to figure out how to delete these lines.

In VIM, I tried the following and it did not execute:

:%s/[0-9]{4}\-[0-9]{2}\-[0-9]{2}/d

Then I get this error: pattern not found.

I can do the following and get results:

egrep '[0-9]{4}\-[0-9]{2}\-[0-9]{2}' file

This prints out:

2006-12-18
2007-02-05
2007-02-13
2007-03-06
2007-03-13
2007-04-03
2007-04-15
2007-04-16
2007-04-24
2007-05-18
2007-06-14
2007-07-15
2008-05-14
2008-05-20
2008-06-03
2008-06-10
2008-06-11
2008-06-19
2008-06-25
2008-07-15
2008-07-21
2009-10-21
2010-07-02

In VIM, I tried adding a ^ to the beginning of the pattern and a $ to the end and it still did not recognize the pattern.

I am using VIM on Mac 10.11.2 (15C50) El Capitan using Terminal.

sed gives me issues as well. I tried:

sed -i '/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/d' file

I get the error:

sed: 1: "file": command c expects \ followed by text

I tried sed with or without -i, using "sed 's/pattern//g' file", and sed -e, but none of these did anything to the file.

I also tried replacing [0-9] with [:digit:], but that did not work either:

sed -i '/[:digit:]{4}\-[:digit:]{2}\-[:digit]{2}/d' file

The results were the following error:

sed: 1: "file": command i expects \ followed by text

Please help. Thank you!

Kent
  • 189,393
  • 32
  • 233
  • 301
technerdius
  • 253
  • 3
  • 6
  • 16

2 Answers2

3
  • You have known BRE, then you should escape { to give it special meaning.
  • sed uses BRE as default, but you can add -r (gnu sed) to let sed work with ERE, then your regex should work.
  • - is not a special char in regex, (unless it sits between [...]), so you don't need escape it
  • with vim, default magic setting is magic, so you need escape { as well
  • what you can do in vim to save those escaping is: :%s/\vpattern.... the \v will let :%s/../../ work with very magic. :h magic to know details.
Kent
  • 189,393
  • 32
  • 233
  • 301
  • That is very helpful. Thank you. – technerdius Dec 22 '15 at 22:01
  • Just to confirm, if I ran `sed '/[0-9]\{4}-[0-9]\{2}-[0-9]\{2}/d' file', would that delete this pattern globally or on the first string? If only the first string, should I add the `g' to the front like VIM such as `sed 'g/[0-9]\{4}-[0-9]\{2}-[0-9]\{2}/d' file'? Thank you!!! – technerdius Dec 23 '15 at 19:37
  • @technerdius first of all, your pattern(regex) is not correct. You should escape `}` as well. The sed line will remove the line containing the pattern. You can test and see the result actually. – Kent Dec 23 '15 at 21:08
  • Yes, I see that. Thank you. – technerdius Dec 23 '15 at 21:56
  • I created a test file called testsed.txt. I contained only those dates and a line at the end `test text.' I used `sed -i.bk '/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}/d' testsed.txt'. Strangely, it did not work with just `-i'. I had to include the `.bk' in order for it to work. – technerdius Dec 23 '15 at 22:15
  • @technerdius very likely you were using bsd sed. – Kent Dec 23 '15 at 23:24
2

In Vim, I think you have the wrong things escaped, and are using the wrong command - s is for substitution, not to run line delete commands; try

:g/\d\{4}-\d\{2}-\d\{2}/d

(You can use [0-9] but \d seems neater - and :help \d says it's faster)

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
  • 1
    @technerdius it is not **like** magic, it **is** `magic`, vim's `magic` – Kent Dec 22 '15 at 21:59
  • and should be noted, `\d` will **not** work with `BRE`, **not** work with `ERE` either. But it will work with `PCRE` – Kent Dec 22 '15 at 22:03
  • It worked on a Mac El Capitan Terminal. So, I guess that is PCRE. Thank you for that information. – technerdius Dec 23 '15 at 19:21
  • I don't know what @Kent is trying to note, but Vim regexes aren't PCRE, BRE or ERE, they come down from a time before those existed ( http://stackoverflow.com/a/13999104/478656 ). Since you said BRE in the title, and mentioned 'sed', maybe it looks like you wanted a BRE-compatible regex, and `\d` in my answer is not compatible with that (I'm not that familiar with the differences, personally). – TessellatingHeckler Dec 23 '15 at 19:31
  • @TessellatingHeckler that is what I meant. `\d` works for vimregex, but it (`\d -> [0-9]`) is not supported by `BRE`. Since OP mentioned BRE in question. – Kent Dec 23 '15 at 21:11