Amazon Interview Question
Software Engineer in TestsCountry: United States
Hi Dinesh,
Good one.
But just a correction or two:
1) '.' matches any single character except end-of-line. So, it has to be escaped when we want to match '.'
2) the strings such as 1234.123.341.243 which has more than 3 digits (1234) should not be included.
so, the final command might look like:
ls -R| grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | grep -v '[0-9]\{4,\}'
-v switch: exclude those that match the pattern ( here 4 or more occurrences of digits)
Use the above command with find command:
find /repository -iname '*.*' | xargs egrep -i '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}'
Find searches (case insensitive) over all the files in /repository, and for each file, performs an egrep.
Glad to see this answer.
What this question is really testing, from the interviewer's perspective, is whether you're good at solving problems in the easiest way possible. Some people will go on to write 200 lines of ad-hoc Java code to parse for the string.
ls |grep "[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}"
- rtpdinesh March 21, 2012