Monday, January 13, 2014

Benefits of learning the Shell from a reformed windows user

A while back I thought I knew quite a bit, coding Java on my Windows XP laptop.  I read the Pragmatic Programmer and it talked about learning a shell and all the things it could help you with.  Of course at the time, I knew better, what would I need a shell to do, a DOS prompt was just fine.  Then in my consulting travels I was lucky enough to work with Victor Gonzales.  He had spent a lot of time working on Unix environments and felt so constrained on Windows that he installed Cygwin.  As I watched him work, I was amazed at what he could do in emacs as well as what he would do from the command prompt.  This prompted me, pun intended to install Cygwin also and start learning this amazing tool.  Over time I migrated to a Mac so I could use terminal, and now like Victor  I think I would be lost if I couldn't have this tool in my arsenal.

Now that I have given all this background on how I started using the Shell, here is the specific case.  I was working on a Grails project and we found an error that was being created in all files that were doing a specific thing.  Rather then look through all the files manually I was able to come up with these scripts to help me identify the affected files and also to then replace the affected lines.

This first script is what I came up with to find all the files with the issue.  I did a grep of all files in the views directory that contained actionSubmit.  I pipe those files to show the line that has action=  I then use sed to remove the action= from the line, sort it, uniq it, then use awk to display the information
grep -R "actionSubmit" views/* | grep -o 'action=\"[a-zA-Z]*' | sed 's/action="//' | sort | uniq | awk '{print "    def " $1 " = { \n        error()\n    }\n";}'
This is similar to the above but I use find to return all the files under views then grep each one for the actionSubmit to get the items and the counts this is then piped through grep for the line and displayed.

find views/ -type f -exec sh -c "grep -l actionSubmit {};  grep -c actionSubmit {}; grep actionSubmit {} | grep -o 'action=\"[a-zA-Z]*' | sed 's/action=\"//'"  \;

No comments:

Post a Comment