Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

2 Answers

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 Shah 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
add comment

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 Shah Apr 28 '11 at 1:18
add comment

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.