Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've recently started learning sed. I did

$seq 10 | sed '/[^049]/d'

I was expecting


as output. But I got


Where am I making mistake in understanding this regex?

share|improve this question
up vote 4 down vote accepted

The 1 in the number 10 matches [^049] so it's deleted.

share|improve this answer
Whoops. That was foolish of me to ask here. :( Thanks by the way. – Dharmit Apr 27 '11 at 2:09
Minor tip: Bracketing your regex with ^ and $ is useful to ensure that you're always looking at the whole string. – l0b0 Apr 27 '11 at 9:48

If you really want to show lines containing '0', '4', or '9', here's how:

seq 10 | sed -n '/[049]/p'

The -n instructs sed to not print any lines. The p command instructs sed to print lines matching the /regex/

Alternatively, you can always use grep :-)

seq 10 | grep -E "[049]"
share|improve this answer
Thanks. That was helpful. :) – Dharmit Apr 28 '11 at 1:18
This is valid, but the exact approach the OP was looking for was probably: seq 10 | sed '/[049]/!d' This deletes any lines that do not match [049] rather than deleting any lines which match "not 049", aka [^049]. – Wildcard Nov 19 '15 at 0:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.