3

I've recently started learning sed. I did

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

I was expecting

4 
9
10

as output. But I got

4
9

Where am I making mistake in understanding this regex?

2 Answers 2

4

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

2
  • Whoops. That was foolish of me to ask here. :( Thanks by the way.
    – Dharmit
    Apr 27, 2011 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, 2011 at 9:48
1

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]"
1
  • 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, 2015 at 0:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .