2 Answers
2

Commands for grep and sed

It looks like the original question was formatted incorrectly. Hence, the simplest grep and sed commands are:

egrep '[0-9]{2}$' /path/to/file

and

sed -rn '/[0-9]{2}$/p' /path/to/file

I'll leave the explanations below, which include information on these commands, but also work with multiple strings on single lines. They will still work, but include additional, potentially extraneous code.

Grep

grep '[^ ]*[0-9]\{2\}( |$)' /path/to/file

Explanation

[^ ]* Match any number of non-space characters.

[0-9]\{2\} Match two digits. (You could use [0-9][0-9] instead.)

( |$) The digits should be the end of a "word", hence should be followed by either a space or an end-of-line terminator.

I prefer using extended grep instead, as you won't need to escape the curly braces. Hence,

grep -E '[^ ]*[0-9]{2}( |$)' /path/to/file

or

egrep '[^ ]*[0-9]{2}( |$)' /path/to/file

The question has been edited, but if there are multiple matches on a line, and you want to only output the matches, then use the -o flag. i.e.

egrep -o '[^ ]*[0-9]{2}( |$)' /path/to/file

Sed

</path/to/file tr ' ' '\n' | sed -rn '/[0-9]{2}$/p'

Explanation

sed -rn Use extended regular expressions (similar to above) with -r, and don't print all the output immediately with -n.

[0-9]{2}$ This regular expression is similar to the one above in the grep line. However, since we've already removed all the spaces, we don't need to define the non-space characters at the beginning, nor the possible space at the end.

Also matches dsassd90897 and only highlights matches, since its grep.
–
polymJun 24 '14 at 14:42

1

@polym, I think they want dsassd90897, right? Also, I'm not sure what output they want... and there's since been an edit to the question. Anyway, I've edited for the last part.
–
SparhawkJun 24 '14 at 14:43

Nice edit (I think, but again the question is unclear :). +1 (For the sed part, I think you could do that in awk? I'm also not sure if tr would be faster, but probably only marginal if anything.)
–
SparhawkJun 24 '14 at 15:01

1

Ha… I was curious, so I did a test. time for i in $(seq 1000000); do echo 'adasfddfd09 dsassd90897 323sdsdsdsd sdddsdf56 dfdf45fdfdf'; done | sed 's/ /\n/g' > /dev/null vs. time for i in $(seq 1000000); do echo 'adasfddfd09 dsassd90897 323sdsdsdsd sdddsdf56 dfdf45fdfdf'; done | tr ' ' '\n' > /dev/null On my computer, the variation between runs for either command is much higher than the difference between the two commands.
–
SparhawkJun 24 '14 at 15:07

it would be nice if awk splits and matches in one line, but I can't find any solution. :(. Is there any test if tr or sed is faster? EDIT: Ha, two guys, same thought!
–
polymJun 24 '14 at 15:07

I always thought that the regex part of sed would be slower (and unnecessary here), but obviously I was wrong.
–
SparhawkJun 24 '14 at 15:08

yeah i tested it on my system with your commands, too :). sed seems to be faster :D
–
polymJun 24 '14 at 15:10