Using PowerShell to Ace the Scripting & Regular Expression Interview Question

Last week I saw a tweet that referenced a couple of interesting articles on interview questions for programmers. I’m no programmer, but one of them (Steve Yegge’s The Five Essential Phone-Screen Questions) had an interesting question about identifying which of 50,000 web pages had phone numbers in them. You have to identify all of the web pages with phone numbers hard coded in them. All of the web pages are HTML files in a directory tree under a folder named website. The phone numbers are in the format of ###-###-#### or (###) ###-####. You have two days to deliver a list of the files with phone numbers in them and their paths. I’m paraphrasing the requirements.

I couple of days after I read this article, I started reading another book on PowerShell (via Safari Books Online) named “Windows PowerShell in Action, Second Edition” by Bruce Payette. The first page in Chapter One had an example of piping Dir to Select-String to do a search for the word “error” in the log files from the Windows directory. This gave me an idea about how to solve the phone number problem. Why not use PowerShell? Displaying help on Select-String confirmed that it is able to use regular expressions.

I also recently learned that if a line ends with a pipe symbol or comma then you don’t need to use the backtick (grave accent) character for line continuation. That tip came from “Learn Windows PowerShell in a Month of Lunches” by Don Jones which is also available on Safari Books Online.

µ

Leave a Reply

%d bloggers like this: