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?


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

  • Whoops. That was foolish of me to ask here. :( Thanks by the way. – Dharmit Apr 27 '11 at 2:09
  • 1
    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]"
  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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