Question: I am new to bash and having a tough time figuring this out.

Using `sed`, could anyone help me in finding only even numbers in a given file?

I figured out how to find all numbers starting from [0,2,4,6,8] using this:

```
sed -n 's/^[0-9]*[02468] /&/w even' <file 

```

But this doesn't guarantee that the number is even for sure.

I am having trouble in finding if the matched number ends with either [0,2,4,6,8] for it to be even for sure.

So can any one help me out with this?

Answer: 
Your command should work fine assuming that there is a space after all even numbers and that they are all at the beginning of the lines:

```
$ echo 'foo 
1231 
2220 
1254 ' | sed -n '/[0-9]*[02468] /p'
2220 
1254 

```

Also note that, as you don't actually do a substitution, you don't need the `s` command. Use an address (pattern) specifier and `w` command (like I did above with the `p` command).

To make sure that the even digit is the last, but is not necessarily followed by a space, you can do something like

```
$ echo 'foo
1231
2220
1254 ' | sed -n '/[0-9]*[02468]\($\|[^0-9]\)/p'
2220
1254 

```

Actually, your case looks more like a use case for `grep`, not `sed`, because you do filtering rather than editing. Everything becomes easier with `GNU grep`, as you can do

```
$ echo 'foo
1231
2220
1254 ' | grep -P '\d*[02468](?!\d)'
2220
1254 

```

Just append `> even` to the command to make it write to the file `even`.
Why don't you find the numbers ending with [02468] ?