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?
The 1
in the number 10
matches [^049]
so it's deleted.
^
and $
is useful to ensure that you're always looking at the whole string.
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]"
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]
.