Find and replace interview question

We’ve recently been searching for a junior sysadmin to join the team (and I’m very happy to say we’ve now succeeded) so as part of my day to day tasks I had to come up with a dozen simple questions to weed out the people that have never used anything but webmin (and there is a surprising number of them out there). One of the questions seemed to cause a lot of trouble in the general sense and tripped up the few who even made an attempt -

How would you change all occurrences of to in a text file?

While most of the candidates failed this one on account of skipping the question we had a couple make attempts (oddly all using the vim replace syntax). While I was hoping for a little bit of sed or perl a solution’s a solution. Unfortunately theirs all had edge cases.

First was the obvious trick in the question. All the answers (apart from one) came back with something like this - %s/ First up we have the missing boundary test. While that regex is fine for it breaks quite badly on (it replaces it with Secondly the dots are unescaped, which means it’s not only going to work on IPs, but number sequences.

I was actually really surprised at how few of the admins could write an awk / perl / shell script and how many paid very little attention to what the regex actually matched. Still, while the question seemed easy enough it had enough awkwardness to enable further discussion about regexs, data checking and the evil of manual changes. And it showed that everybody likes vim :)